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

一、ndarray.astype(type)

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

stock_change.astype(np.int32)

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

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

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

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`.

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

创建配置文件

jupyter notebook --generate-config
vi ~/.jupyter/jupyter_notebook_config.py

取消注释,多增加

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

但是不建议这样去修改,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))

打印结果:

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
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值