使用NumPy的数据类型的大小 [英] Size of data type using NumPy
问题描述
在NumPy中,我可以通过以下方式获取特定数据类型的大小(以字节为单位):
In NumPy, I can get the size (in bytes) of a particular data type by:
datatype(...).itemsize
或:
datatype(...).nbytes
例如:
np.float32(5).itemsize #4
np.float32(5).nbytes #4
我有两个问题.首先,有没有一种方法可以在不创建数据类型实例的情况下获取此信息?其次,itemsize
和nbytes
有什么区别?
I have two questions. First, is there a way to get this information without creating an instance of the datatype? Second, what's the difference between itemsize
and nbytes
?
推荐答案
您需要dtype
的实例来获取项目大小,但是您不需要ndarray
的实例. (很快就会知道,nbytes
是数组的属性,而不是dtype.)
You need an instance of the dtype
to get the itemsize, but you shouldn't need an instance of the ndarray
. (As will become clear in a second, nbytes
is a property of the array, not the dtype.)
例如
print np.dtype(float).itemsize
print np.dtype(np.float32).itemsize
print np.dtype('|S10').itemsize
就itemsize
和nbytes
之间的差异而言,nbytes
只是x.itemsize * x.size
.
As far as the difference between itemsize
and nbytes
, nbytes
is just x.itemsize * x.size
.
例如
In [16]: print np.arange(100).itemsize
8
In [17]: print np.arange(100).nbytes
800
这篇关于使用NumPy的数据类型的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!