python - 将 dtype 从 int64 转换为 int32

标签 python numpy

基本上,我使用 python x32 位从文件加载一个包含多个 numpy 数组的列表对象(之前使用 python x64 保存在 pickle 中)。

我可以正确加载它们并检查内容,但我无法使用它们。

TypeError: Cannot cast array data from dtype('int64') to dtype('int32')

如何将列表中的数组元素类型转换为 int32,以便我可以将它们与 python x32 一起使用。

当我尝试执行以下部分时出现错误:

a=np.bincount(np.hstack(data['Y']))

查看 data['Y'] 里面的内容 enter image description here

最佳答案

正如其他人所说,32 位版本的 numpy 仍然支持 64 位数据类型。但是如果你真的需要转换成int32,你可以使用astype功能:

>>> import numpy as np
>>> x = np.array([1,2,3], dtype=np.int64)
>>> x
array([1, 2, 3])
>>> x.dtype
dtype('int64')
>>> y = x.astype(np.int32)
>>> y
array([1, 2, 3], dtype=int32)

关于python - 将 dtype 从 int64 转换为 int32,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22725043/

相关文章:

python - 为什么这个 Python 代码会破坏我的计算机?

oop - 根据初始化参数更改实例化的类

python - 带 GPS 数据的加权 K 均值

python - 将列表的一维对象 numpy 数组转换为二维数值数组并返回

python - 在认知服务 API 中创建人员组时出现问题

python - Flask 文件结构

python - 仅针对某些应用程序收集静态信息

python - 从 numpy 数组中删除连续的数字

python - 使用 pandas 在 python 中 reshape CSV 数据

numpy - 如何使 scipy.ndimage.zoom 的 UserWarning 静音