In NumPy, I can get the size (in bytes) of a particular data type by:
在NumPy中,我可以通过以下方式获取特定数据类型的大小(以字节为单位):
datatype(...).itemsize
or:
datatype(...).nbytes
For example:
np.float32(5).itemsize #4
np.float32(5).nbytes #4
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
?
我有两个问题。首先,有没有办法在不创建数据类型实例的情况下获取此信息?第二,itemsize和nbytes之间的区别是什么?
2 个解决方案
#1
39
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.)
您需要一个dtype实例来获取itemsize,但您不需要ndarray的实例。 (如果在一秒钟内变得清晰,nbytes是数组的属性,而不是dtype。)
E.g.
print np.dtype(float).itemsize
print np.dtype(np.float32).itemsize
print np.dtype('|S10').itemsize
As far as the difference between itemsize
and nbytes
, nbytes
is just x.itemsize * x.size
.
至于itemsize和nbytes之间的区别,nbytes只是x.itemsize * x.size。
E.g.
In [16]: print np.arange(100).itemsize
8
In [17]: print np.arange(100).nbytes
800
#2
13
Looking at the NumPy C source file, this is the comment:
查看NumPy C源文件,这是评论:
size : int
Number of elements in the array.
itemsize : int
The memory use of each array element in bytes.
nbytes : int
The total number of bytes required to store the array data,
i.e., ``itemsize * size``.
So in NumPy:
所以在NumPy中:
>>> x = np.zeros((3, 5, 2), dtype=np.float64)
>>> x.itemsize
8
So .nbytes
is a shortcut for:
所以.nbytes是一个捷径:
>>> np.prod(x.shape)*x.itemsize
240
>>> x.nbytes
240
So, to get a base size of a NumPy array without creating an instance of it, you can do this (assuming a 3x5x2 array of doubles for example):
因此,要获得NumPy数组的基本大小而不创建它的实例,您可以这样做(假设例如3x5x2的双精度数组):
>>> np.float64(1).itemsize * np.prod([3,5,2])
240
However, important note from the NumPy help file:
但是,来自NumPy帮助文件的重要说明:
| nbytes
| Total bytes consumed by the elements of the array.
|
| Notes
| -----
| Does not include memory consumed by non-element attributes of the
| array object.
#1
39
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.)
您需要一个dtype实例来获取itemsize,但您不需要ndarray的实例。 (如果在一秒钟内变得清晰,nbytes是数组的属性,而不是dtype。)
E.g.
print np.dtype(float).itemsize
print np.dtype(np.float32).itemsize
print np.dtype('|S10').itemsize
As far as the difference between itemsize
and nbytes
, nbytes
is just x.itemsize * x.size
.
至于itemsize和nbytes之间的区别,nbytes只是x.itemsize * x.size。
E.g.
In [16]: print np.arange(100).itemsize
8
In [17]: print np.arange(100).nbytes
800
#2
13
Looking at the NumPy C source file, this is the comment:
查看NumPy C源文件,这是评论:
size : int
Number of elements in the array.
itemsize : int
The memory use of each array element in bytes.
nbytes : int
The total number of bytes required to store the array data,
i.e., ``itemsize * size``.
So in NumPy:
所以在NumPy中:
>>> x = np.zeros((3, 5, 2), dtype=np.float64)
>>> x.itemsize
8
So .nbytes
is a shortcut for:
所以.nbytes是一个捷径:
>>> np.prod(x.shape)*x.itemsize
240
>>> x.nbytes
240
So, to get a base size of a NumPy array without creating an instance of it, you can do this (assuming a 3x5x2 array of doubles for example):
因此,要获得NumPy数组的基本大小而不创建它的实例,您可以这样做(假设例如3x5x2的双精度数组):
>>> np.float64(1).itemsize * np.prod([3,5,2])
240
However, important note from the NumPy help file:
但是,来自NumPy帮助文件的重要说明:
| nbytes
| Total bytes consumed by the elements of the array.
|
| Notes
| -----
| Does not include memory consumed by non-element attributes of the
| array object.