回答问题

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

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

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

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

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

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

看看里面是什么data zwz100005 'Y']在此处输入图像描述

Answers

正如其他人所说,32 位版本的 numpy 仍然支持 64 位 dtypes。但是如果真的需要转成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)
Logo

Python社区为您提供最前沿的新闻资讯和知识内容

更多推荐