How to use size and itemsize attributes of NumPy array In Python

size: This attribute gives the number of elements present in the NumPy array.

itemsize: This attribute gives the memory size of one element of NumPy array in bytes.

Example 1:

Python3




# import library
import numpy as np
 
# create a numpy 1d-array
x = np.array([100,20,34])
 
print("Size of the array: ",
      x.size)
 
print("Memory size of one array element in bytes: ",
      x.itemsize)
 
# memory size of numpy array in bytes
print("Memory size of numpy array in bytes:",
      x.size * x.itemsize)


Output:

Size of the array:  3
Memory size of one array element in bytes:  4
Memory size of numpy array in bytes: 12

Example 2:

Python3




# import library
import numpy as np
 
# create a numpy 2d-array
x = np.array([[100, 20, 34],
              [300, 400, 600]])
 
print("Size of the array: ",
      x.size)
 
print("Memory size of one array element in bytes: ",
      x.itemsize)
 
# memory size of numpy array
print("Memory size of numpy array in bytes:",
      x.size * x.itemsize)


Output:

Size of the array:  6
Length of one array element in bytes:  4
Memory size of numpy array in bytes: 24

Find the memory size of a NumPy array

In this post, we will see how to find the memory size of a NumPy array. So for finding the memory size of a NumPy array we are using following methods:

Similar Reads

Using size and itemsize attributes of NumPy array

size: This attribute gives the number of elements present in the NumPy array. itemsize: This attribute gives the memory size of one element of NumPy array in bytes....

Using nbytes attribute of NumPy array.

...