本文介绍了本机 int 类型和 numpy.int 类型有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您能否帮助理解原生 int 类型与 numpy.int32 或 numpy.int64 类型之间的主要区别(如果有)?

Can you please help understand what are the main differences (if any) between the native int type and the numpy.int32 or numpy.int64 types?

推荐答案

另一种看待差异的方法是问2种对象有什么方法.

Another way to look at the differences is to ask what methods do the 2 kinds of objects have.

在 Ipython 中,我可以使用 tab complete 来查看方法:

In Ipython I can use tab complete to look at methods:

In [1277]: x=123; y=np.int32(123)

int 方法和属性:

In [1278]: x.<tab>
x.bit_length   x.denominator  x.imag         x.numerator    x.to_bytes
x.conjugate    x.from_bytes   x.real

int 'operators'

int 'operators'

In [1278]: x.__<tab>
x.__abs__           x.__init__          x.__rlshift__
x.__add__           x.__int__           x.__rmod__
x.__and__           x.__invert__        x.__rmul__
x.__bool__          x.__le__            x.__ror__
...
x.__gt__            x.__reduce_ex__     x.__xor__
x.__hash__          x.__repr__
x.__index__         x.__rfloordiv__

np.int32 方法和属性(或属性).一些相同,但更多,基本上都是 ndarray 的:

np.int32 methods and attributes (or properties). Some of the same, but a lot more, basically all the ndarray ones:

In [1278]: y.<tab>
y.T             y.denominator   y.ndim          y.size
y.all           y.diagonal      y.newbyteorder  y.sort
y.any           y.dtype         y.nonzero       y.squeeze
...
y.cumsum        y.min           y.setflags
y.data          y.nbytes        y.shape

y.__ 方法看起来很像 int 方法.他们可以做同样的数学.

the y.__ methods look a lot like the int ones. They can do the same math.

In [1278]: y.__<tab>
y.__abs__              y.__getitem__          y.__reduce_ex__
y.__add__              y.__gt__               y.__repr__
...
y.__format__           y.__rand__             y.__subclasshook__
y.__ge__               y.__rdivmod__          y.__truediv__
y.__getattribute__     y.__reduce__           y.__xor__

y 在很多方面与 0d 数组相同.不完全相同,但接近.

y is in many ways the same as a 0d array. Not identical, but close.

In [1281]: z=np.array(123,dtype=np.int32)

np.int32 是我索引那种类型的数组时得到的:

np.int32 is what I get when I index an array of that type:

In [1300]: A=np.array([0,123,3])

In [1301]: A[1]
Out[1301]: 123

In [1302]: type(A[1])
Out[1302]: numpy.int32

我必须使用 item 删除所有的 numpy 包装.

I have to use item to remove all of the numpy wrapping.

In [1303]: type(A[1].item())
Out[1303]: int

作为 numpy 用户,np.int32 是带有 numpy 包装器的 int.或者相反,ndarray 的单个元素.通常我不会注意 A[0] 是给我原生"int 还是 numpy 等效项.对比一些新用户,我很少用np.int32(123);我会用 np.array(123) 代替.

As a numpy user, an np.int32 is an int with a numpy wrapper. Or conversely a single element of an ndarray. Usually I don't pay attention as to whether A[0] is giving me the 'native' int or the numpy equivalent. In contrast to some new users, I rarely use np.int32(123); I would use np.array(123) instead.

A = np.array([1,123,0], np.int32)

不包含 3 个 np.int32 对象.相反,它的数据缓冲区是 3*4=12 字节长.数组开销将其解释为 1d 中的 3 个整数.并且 view 向我展示了具有不同解释的相同数据缓冲区:

does not contain 3 np.int32 objects. Rather its data buffer is 3*4=12 bytes long. It's the array overhead that interprets it as 3 ints in a 1d. And view shows me the same databuffer with different interpretations:

In [1307]: A.view(np.int16)
Out[1307]: array([  1,   0, 123,   0,   0,   0], dtype=int16)

In [1310]: A.view('S4')
Out[1310]: array([b'x01', b'{', b''],   dtype='|S4')

只有当我索引单个元素时,我才会得到一个 np.int32 对象.

It's only when I index a single element that I get a np.int32 object.

列表L=[1, 123, 0] 不同;它是一个指针列表 - 指向内存中其他位置的 int 对象的指针.同样对于 dtype=object 数组.

The list L=[1, 123, 0] is different; it's a list of pointers - pointers to int objects else where in memory. Similarly for a dtype=object array.

这篇关于本机 int 类型和 numpy.int 类型有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-09 17:00