In NumPy, I can get the size (in bytes) of a particular data type by:
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
?
size() function count the number of elements along a given axis.
Size of the first dimension of the NumPy array: len() len() is the Python built-in function that returns the number of elements in a list or the number of characters in a string. For numpy. ndarray , len() returns the size of the first dimension.
In the case of CPython (the most common Python implementation), every float object will contain a reference counter and a pointer to the type (a pointer to the float class), which will each be 8 bytes for 64bit CPython or 4 bytes each for 32bit CPython.
The data type can be specified using a string, like 'f' for float, 'i' for integer etc. or you can use the data type directly like float for float and int for integer.
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.)
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
.
E.g.
In [16]: print np.arange(100).itemsize 8 In [17]: print np.arange(100).nbytes 800
Looking at the NumPy C source file, this is the comment:
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:
>>> x = np.zeros((3, 5, 2), dtype=np.float64) >>> x.itemsize 8
So .nbytes
is a shortcut for:
>>> 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):
>>> np.float64(1).itemsize * np.prod([3,5,2]) 240
However, important note from the NumPy help file:
| nbytes | Total bytes consumed by the elements of the array. | | Notes | ----- | Does not include memory consumed by non-element attributes of the | array object.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With