Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
393 views
in Technique[技术] by (71.8m points)

python - How does numpy determine the array data type when it contains multiple dtypes?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...