为什么在 NumPy 中使用楼层划分时会显示 dtype(即使它是本机类型)?



通常,当dtype等效于本机类型时,它会隐藏:

>>> import numpy as np
>>> np.arange(5)
array([0, 1, 2, 3, 4])
>>> np.arange(5).dtype
dtype('int32')
>>> np.arange(5) + 3
array([3, 4, 5, 6, 7])

但不知何故,这不适用于地板划分或模:

>>> np.arange(5) // 3
array([0, 0, 0, 1, 1], dtype=int32)
>>> np.arange(5) % 3
array([0, 1, 2, 0, 1], dtype=int32)

为什么会有区别?

Python 3.5.4, NumPy 1.13.1, Windows 64bit

这里实际上有多个不同的 32 位整数 dtype。这可能是一个错误。

NumPy(意外地?)创建了多个不同的有符号32位整数类型,可能对应于Cintlong。它们都显示为numpy.int32,但它们实际上是不同的对象。在C级别,我相信类型对象是PyIntArrType_TypePyLongArrType_Type的,在这里生成。

dtype 对象具有与该 dtype 的标量的类型对象对应的type属性。NumPy在决定是否在数组repr中打印dtype信息时检查的正是这个type属性:

_typelessdata = [int_, float_, complex_]
if issubclass(intc, int):
_typelessdata.append(intc)

if issubclass(longlong, int):
_typelessdata.append(longlong)
...
def array_repr(arr, max_line_width=None, precision=None, suppress_small=None):
...
skipdtype = (arr.dtype.type in _typelessdata) and arr.size > 0
if skipdtype:
return "%s(%s)" % (class_name, lst)
else:
...
return "%s(%s,%sdtype=%s)" % (class_name, lst, lf, typename)

numpy.arange(5)numpy.arange(5) + 3.dtype.typenumpy.int_;在numpy.arange(5) // 3numpy.arange(5) % 3.dtype.type是另一种32位有符号整数类型。

至于为什么+//具有不同的输出dtype,它们使用不同的类型解析例程。这是//的,这是+的。//的类型解析查找一个 ufunc 内部循环,该循环采用输入可以安全转换为的类型,而+的类型解析将 NumPy 类型提升应用于参数并选择与结果类型匹配的循环。

它归结为dtype的差异,从view可以看出:

In [186]: x = np.arange(10)
In [187]: y = x // 3
In [188]: x
Out[188]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
In [189]: y
Out[189]: array([0, 0, 0, 1, 1, 1, 2, 2, 2, 3], dtype=int32)
In [190]: x.view(y.dtype)
Out[190]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], dtype=int32)
In [191]: y.view(x.dtype)
Out[191]: array([0, 0, 0, 1, 1, 1, 2, 2, 2, 3])

尽管dtypedescr相同,但有些属性是不同的。 但是呢?

In [192]: x.dtype.descr
Out[192]: [('', '<i4')]
In [193]: y.dtype.descr
Out[193]: [('', '<i4')]
In [204]: x.dtype.type
Out[204]: numpy.int32
In [205]: y.dtype.type
Out[205]: numpy.int32
In [207]: dtx.type is dty.type
Out[207]: False
In [243]: np.core.numeric._typelessdata
Out[243]: [numpy.int32, numpy.float64, numpy.complex128]
In [245]: x.dtype.type in np.core.numeric._typelessdata
Out[245]: True
In [246]: y.dtype.type in np.core.numeric._typelessdata
Out[246]: False

所以y从表面上看dtype.typexs是一样的,但它是一个不同的对象,具有不同的id

In [261]: id(np.int32)
Out[261]: 3045777728
In [262]: id(x.dtype.type)
Out[262]: 3045777728
In [263]: id(y.dtype.type)
Out[263]: 3045777952
In [282]: id(np.intc)
Out[282]: 3045777952

将此额外的type添加到列表中,y不再显示 dtype:

In [267]: np.core.numeric._typelessdata.append(y.dtype.type)
In [269]: y
Out[269]: array([0, 0, 0, 1, 1, 1, 2, 2, 2, 3])

所以y.dtype.typenp.intc(和np.intp),而x.dtype.typenp.int32(和np.int_)。

因此,要创建一个显示 dtype 的数组,请使用np.intc.

In [23]: np.arange(10,dtype=np.int_)
Out[23]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
In [24]: np.arange(10,dtype=np.intc)
Out[24]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], dtype=int32)

要关闭此功能,请将np.intc附加到np.core.numeric._typelessdata.

相关内容

最新更新