当前位置:   article > 正文

NumPy(九):类型转换【ndarray.astype(type)】【ndarray.tostring([order])】【ndarray.tobytes([order]) 】_np.ndarray.astype

np.ndarray.astype

一、ndarray.astype(type)

返回修改了类型之后的数组

stock_change.astype(np.int32)
  • 1

二、ndarray.tostring([order])或者ndarray.tobytes([order])

构造包含数组中原始数据字节的Python字节

arr = np.array([[[1, 2, 3], [4, 5, 6]], [[12, 3, 34], [5, 6, 7]]])
arr.tostring()
  • 1
  • 2

jupyter输出太大可能导致崩溃问题

如果遇到

IOPub data rate exceeded.
    The notebook server will temporarily stop sending output
    to the client in order to avoid crashing it.
    To change this limit, set the config variable
    `--NotebookApp.iopub_data_rate_limit`.
  • 1
  • 2
  • 3
  • 4
  • 5

这个问题是在jupyer当中对输出的字节数有限制,需要去修改配置文件

创建配置文件

jupyter notebook --generate-config
vi ~/.jupyter/jupyter_notebook_config.py
  • 1
  • 2

取消注释,多增加

## (bytes/sec) Maximum rate at which messages can be sent on iopub before they
#  are limited.
c.NotebookApp.iopub_data_rate_limit = 10000000
  • 1
  • 2
  • 3

但是不建议这样去修改,jupyter输出太大会崩溃

案例

import numpy as np

# 数组类型转换:.astype()

ar1 = np.arange(10, dtype=float)
print('ar1 = {0}, ar1.dtype = {1}'.format(ar1, ar1.dtype))
print('-' * 100)

# a.astype():转换数组类型
# 注意:养成好习惯,数组类型用np.int32,而不是直接int32
ar2 = ar1.astype(np.int32)  # 可以在参数位置设置数组类型
print('ar2 = {0}, ar2.dtype = {1}'.format(ar2, ar2.dtype))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

打印结果:

ar1 = [0. 1. 2. 3. 4. 5. 6. 7. 8. 9.], ar1.dtype = float64
----------------------------------------------------------------------------------------------------
ar2 = [0 1 2 3 4 5 6 7 8 9], ar2.dtype = int32

Process finished with exit code 0
  • 1
  • 2
  • 3
  • 4
  • 5
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/742254
推荐阅读
相关标签
  

闽ICP备14008679号