🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEnumpy

numpy Documentation

LOADING ENGINE...

ndarray.nbytes

AI & DATA SCIENCE // ndarray-nbytes

ndarray.nbytes is the total number of bytes consumed by the array's actual element data, equal to its size multiplied by its itemsize.

Syntax

arr.nbytes

Deep Dive Course

nbytes reflects only the raw element data stored in the array's underlying buffer — it doesn't include the small amount of additional memory Python and NumPy use for the array object itself, its shape, dtype, and other metadata, so it's a slight underestimate of the object's true total memory footprint, though for any array of meaningful size, the metadata overhead is negligible in comparison. It's a quick, direct way to estimate whether a planned array will fit comfortably in available memory before actually allocating it.

1Understanding ndarray.nbytes

nbytes reflects only the raw element data stored in the array's underlying buffer — it doesn't include the small amount of additional memory Python and NumPy use for the array object itself, its shape, dtype, and other metadata, so it's a slight underestimate of the object's true total memory footprint, though for any array of meaningful size, the metadata overhead is negligible in comparison. It's a quick, direct way to estimate whether a planned array will fit comfortably in available memory before actually allocating it.

💡

Check .nbytes before allocating a very large array, or several of them, to sanity-check that the numbers make sense — it's an easy way to catch an accidental extra zero in a shape calculation before it crashes the program with a memory error.

editor.html
import numpy as np

arr = np.zeros((1000, 1000), dtype=np.float64)
print(arr.nbytes)
localhost:3000

2Practical Example

Here is a real-world application of ndarray.nbytes showing how it is used in production NumPy code.

editor.html
import numpy as np

big = np.zeros(1_000_000, dtype=np.float64)
small = big.astype(np.float32)
print(big.nbytes, small.nbytes)
localhost:3000

3Best Practices

Follow these guidelines when working with ndarray.nbytes:

1. Check .nbytes before allocating unusually large arrays, to catch shape miscalculations before they exhaust available memory

2. Choose a smaller dtype deliberately when nbytes matters, since it directly scales with itemsize

3. Remember .nbytes measures only the array's raw data, not the small additional overhead of the Python/NumPy object wrapping it

⚠️

Tip: Check .nbytes before allocating a very large array, or several of them, to sanity-check that the numbers make sense — it's an easy way to catch an accidental extra zero in a shape calculation before it crashes the program with a memory error.

editor.html
import numpy as np

arr = np.zeros((1000, 1000), dtype=np.float64)
print(arr.nbytes)
localhost:3000

Examples

Example 01Basic Usage
import numpy as np

arr = np.zeros((1000, 1000), dtype=np.float64)
print(arr.nbytes)
Example 02Advanced Example
import numpy as np

big = np.zeros(1_000_000, dtype=np.float64)
small = big.astype(np.float32)
print(big.nbytes, small.nbytes)

Best Practices

  • Check .nbytes before allocating unusually large arrays, to catch shape miscalculations before they exhaust available memory
  • Choose a smaller dtype deliberately when nbytes matters, since it directly scales with itemsize
  • Remember .nbytes measures only the array's raw data, not the small additional overhead of the Python/NumPy object wrapping it

Interview Question

Why does converting an array's dtype from float64 to float32 with .astype() cut its .nbytes roughly in half?

Hint: Think about how nbytes is actually calculated.

nbytes is calculated as the total number of elements multiplied by the size of each element, itemsize, in the current dtype. float64 uses 8 bytes per element, while float32 uses 4, half as much, so converting an array from float64 to float32 with the same number of elements halves its itemsize and therefore roughly halves its total nbytes, at the cost of reduced numeric precision per element.

Exercises

MediumPractice using ndarray.nbytes in a real scenario.
View Solution
import numpy as np

arr = np.zeros((1000, 1000), dtype=np.float64)
print(arr.nbytes)

Frequently Asked Questions

Why does converting an array's dtype from float64 to float32 with .astype() cut its .nbytes roughly in half?

nbytes is calculated as the total number of elements multiplied by the size of each element, itemsize, in the current dtype. float64 uses 8 bytes per element, while float32 uses 4, half as much, so converting an array from float64 to float32 with the same number of elements halves its itemsize and therefore roughly halves its total nbytes, at the cost of reduced numeric precision per element.

Related Functions

ndarray-itemsizendarray-sizendarray-dtype