如何确定数组数据类型

如何确定数组数据类型

本文介绍了当numpy包含多个dtypes时,如何确定数组数据类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试动手执行numpy,在遇到以下数据类型时会碰到使用内置方法dtype.以下是我得到的一些结果.你能解释一下u11意味着什么吗?

I am trying to do hands on the numpy, i cam across following datatype whenused 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

o/p = >U11

推荐答案

PyArrayObject 类型具有 NPY_PRIORITY 属性,该属性表示应视为数组dtype的类型的优先级如果它包含具有不同数据类型的项目.您可以使用 PyArray_GetPriority API,该API返回 __array_priority__ 属性(转换为双精度),则该名称或属性不存在.在这种情况下,Unicode比整数类型具有更高的优先级,这就是a1.dtype返回U11的原因.

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.

现在,关于U11或通常的U#,它由两部分组成. U表示Unicode dtype,而#表示它可以容纳的元素数.不过,在不同的平台上,这可能会有所不同.

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

在文档 https://docs.scipy.org/doc/numpy-1.14.0/reference/arrays.dtypes.html#data-type-objects-dtype .

这篇关于当numpy包含多个dtypes时,如何确定数组数据类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 22:51