Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Size of data type using NumPy

Tags:

python

numpy

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?

like image 874
mgilson Avatar asked Jun 06 '13 21:06

mgilson


People also ask

What does size () do in NumPy?

size() function count the number of elements along a given axis.

How can I get NumPy size?

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.

What is the size of data types in Python?

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.

What is the data type of NumPy?

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.


2 Answers

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 
like image 195
Joe Kington Avatar answered Sep 30 '22 02:09

Joe Kington


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. 
like image 29
dawg Avatar answered Sep 30 '22 02:09

dawg