The NumPy ndarray nbytes() function return the total bytes consumed by the elements of the array.
Notes
- It returns the total bytes consumed by the total element of the array, but it does not calculate memory consumed by non-element attributes of the array object.
- The size of the data element depends on its attribute.
- It calculates the total bytes consumed by multiplying the shape of the array and the size of one array element.
Syntax
Syntax: numpy.ndarray.nbytes(arr)
Parameters
- arr : [array_like] Input array.
Return
- [int] Total bytes consumed by the elements of the array.
Examples
Let's see an example of how to use ndarray nbytes() method of NumPy library in Python.
Example 1
import numpy as np
arr = np.zeros((1, 2, 3), dtype = np.complex128)
bytes_consumed = arr.nbytes
print (bytes_consumed)
Output :
96
Example 2
# Python program explaining
# numpy.ndarray.nbytes() function
# importing numpy as geek
import numpy as geek
arr = geek.random.rand(10000, 50)
gfg = arr.nbytes
print (gfg)
Output :
4000000