简体   繁体   English

numpy.random的数据类型

[英]Data type for numpy.random

Why does the following numpy.random command not return array with data type of 'int' as I understand should happen from the documentation. 为什么以下numpy.random命令不返回数据类型为'int'的数组,因为我理解应该从文档中发生。

import numpy

b = numpy.random.randint(1,100,10,dtype='int')

type(b[1])

class 'numpy.int64'

I can't change the type using .astype() either. 我也无法使用.astype()更改类型。

In Numpy type system (different from Python built-in types), 'int' translates to 'int64' by default. 在Numpy类型系统中(与Python内置类型不同),'int'默认转换为'int64'。

Try using 'int32', 'int16', ... and you should see it changed. 尝试使用'int32','int16',...你应该看到它改变了。 Or b.astype('float')... 或者b.astype('浮动')......

numpy uses it's own int types, since a python (3) int can be of arbitrary size. numpy使用它自己的int类型,因为python(3) int可以是任意大小。 In other words, you can't have an array of Python ints, best you can do is object dtype. 换句话说,你不能有一个Python int数组,你可以做的最好是对象dtype。 Observe: 注意:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM