How does numpy determin the object-array's dty

2019-01-12 11:35发布

I am trying to do hands on the numpy, i cam across following datatype when used inbuilt method dtype.Following the few results i have got. Can you please explain what it means by u11

a1 = np.array([3,5,'p'])
print(a1.dtype)

o/p = >U11

1条回答
做个烂人
2楼-- · 2019-01-12 12:15

Numpy's array objects that are PyArrayObject types have a NPY_PRIORITY attribute that denotes the priority of the type in which should be considered as the array's dtype in cases that it contains items with heterogeneous data types. You can access to this priority using PyArray_GetPriority API which Returns the __array_priority__ attribute (converted to a double) of obj or def if no attribute of that name exists. In this case Unicode has a more priority than integer type and that's why a1.dtype returns U11.

Now, regarding the U11 or in general U#, it consists of two parts. The U which denotes a Unicode dtype and the # denotes the number of elements it can hold. This may be different in different platforms though.

In [45]: a1.dtype
Out[45]: dtype('<U21')  # 64bit Linux

In [46]: a1.dtype.type  # The type object used to instantiate a scalar of this data-type. 
Out[46]: numpy.str_

In [49]: a1.dtype.itemsize
Out[49]: 84 # 21 * 4

Read more info in greater details about string types and other datatype objects in documentation https://docs.scipy.org/doc/numpy-1.14.0/reference/arrays.dtypes.html#data-type-objects-dtype.

查看更多
登录 后发表回答