[英]Data type for numpy.random
为什么以下numpy.random命令不返回数据类型为'int'的数组,因为我理解应该从文档中发生。
import numpy
b = numpy.random.randint(1,100,10,dtype='int')
type(b[1])
class 'numpy.int64'
我也无法使用.astype()
更改类型。
在Numpy类型系统中(与Python内置类型不同),'int'默认转换为'int64'。
尝试使用'int32','int16',...你应该看到它改变了。 或者b.astype('浮动')......
numpy
使用它自己的int类型,因为python(3) int
可以是任意大小。 换句话说,你不能有一个Python int数组,你可以做的最好是对象dtype。 注意:
In [6]: x = np.array([1,2,3],dtype=np.dtype('O'))
In [7]: x[0]
Out[7]: 1
In [8]: type(x[0])
Out[8]: int
In [9]: x2 = np.array([1,2,3],dtype=int)
In [10]: x2
Out[10]: array([1, 2, 3])
In [11]: x2[0]
Out[11]: 1
In [12]: type(x2[0])
Out[12]: numpy.int64
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.