简体   繁体   中英

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.

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.

In Numpy type system (different from Python built-in types), 'int' translates to 'int64' by default.

Try using 'int32', 'int16', ... and you should see it changed. Or b.astype('float')...

numpy uses it's own int types, since a python (3) int can be of arbitrary size. In other words, you can't have an array of Python ints, best you can do is object 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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