[英]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.