How to use nbytes attribute of NumPy array. In Python

nbytes: This attribute gives the total bytes consumed by the elements of the NumPy array.

Example 1:

Python3




import numpy as n
from sys import getsizeof
 
l = [0] * 10
a = np.array(l)
 
print(getsizeof(l))
 
print(l.nbytes)


Output:

136
40

Example 2:

Python3




# import library
import numpy as np
 
# create numpy 1d-array
x = np.array([100, 20, 34])
 
print("Memory size of a NumPy array:",
      x.nbytes)


Output:

Memory size of a NumPy array: 12

Example 3:

Python3




# import library
import numpy as np
 
# create numpy 2d-array
x = np.array([[100, 20, 34],
              [300, 400, 600]])
 
print("Memory size of a NumPy array:",
      x.nbytes)


Output:

Memory size of a NumPy array: 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.

...