Find the size of numpy Array

NumPy arrays are fundamental data structures for scientific computing in Python. When working with large datasets, it's crucial to understand how much memory your arrays consume. NumPy provides several methods to determine both the number of elements and memory usage of arrays.

Types of NumPy Arrays

NumPy supports two main types of arrays:

  • 1D Arrays Single dimensional arrays storing elements linearly

  • Multi-dimensional Arrays Arrays with nested structure (2D, 3D, etc.)

Understanding Array Size vs Memory Size

There are two important measurements for NumPy arrays:

  • Array size Total number of elements in the array

  • Memory size Actual memory space occupied in bytes

Method 1: Using size and itemsize Properties

The size property returns the total number of elements, while itemsize returns the memory space (in bytes) occupied by each element.

Syntax

numpy.size(array)     # Total number of elements
array.itemsize        # Bytes per element

Example with 1D Array

import numpy as np

# Create a 1D numpy array
arr = np.array([1, 2, 3, 4, 5])

# Display array information
print(f"Array: {arr}")
print(f"Number of elements: {np.size(arr)}")
print(f"Bytes per element: {arr.itemsize}")
print(f"Total memory usage: {arr.itemsize * np.size(arr)} bytes")
Array: [1 2 3 4 5]
Number of elements: 5
Bytes per element: 8
Total memory usage: 40 bytes

Example with Multi-dimensional Array

import numpy as np

# Create 2D and 3D arrays
arr_2d = np.array([[1, 2, 3], [4, 5, 6]])
arr_3d = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])

# 2D array information
print("2D Array:")
print(f"Shape: {arr_2d.shape}")
print(f"Number of elements: {np.size(arr_2d)}")
print(f"Bytes per element: {arr_2d.itemsize}")
print(f"Total memory: {arr_2d.itemsize * np.size(arr_2d)} bytes")

print("\n3D Array:")
print(f"Shape: {arr_3d.shape}")
print(f"Number of elements: {np.size(arr_3d)}")
print(f"Bytes per element: {arr_3d.itemsize}")
print(f"Total memory: {arr_3d.itemsize * np.size(arr_3d)} bytes")
2D Array:
Shape: (2, 3)
Number of elements: 6
Bytes per element: 8
Total memory: 48 bytes

3D Array:
Shape: (2, 2, 2)
Number of elements: 8
Bytes per element: 8
Total memory: 64 bytes

Method 2: Using nbytes Property

The nbytes property directly returns the total memory consumption of the entire array in bytes, eliminating the need for manual calculation.

Syntax

array.nbytes    # Total bytes occupied by array

Example

import numpy as np

# Create arrays with different data types
int_array = np.array([1, 2, 3, 4, 5], dtype=np.int32)
float_array = np.array([1.1, 2.2, 3.3, 4.4, 5.5], dtype=np.float64)
str_array = np.array(['a', 'b', 'c', 'd'], dtype='U1')

print("Integer array (int32):")
print(f"Elements: {np.size(int_array)}")
print(f"Total memory: {int_array.nbytes} bytes")
print(f"Bytes per element: {int_array.itemsize}")

print("\nFloat array (float64):")
print(f"Elements: {np.size(float_array)}")
print(f"Total memory: {float_array.nbytes} bytes")
print(f"Bytes per element: {float_array.itemsize}")

print("\nString array (U1):")
print(f"Elements: {np.size(str_array)}")
print(f"Total memory: {str_array.nbytes} bytes")
print(f"Bytes per element: {str_array.itemsize}")
Integer array (int32):
Elements: 5
Total memory: 20 bytes
Bytes per element: 4

Float array (float64):
Elements: 5
Total memory: 40 bytes
Bytes per element: 8

String array (U1):
Elements: 4
Total memory: 16 bytes
Bytes per element: 4

Comparison of Methods

Property Returns Use Case
numpy.size() Number of elements Count total elements
array.itemsize Bytes per element Memory per single element
array.nbytes Total bytes Complete memory usage

Conclusion

Use numpy.size() and itemsize when you need detailed information about individual elements and total count. Use nbytes for a quick total memory assessment. Understanding memory usage is essential for optimizing performance in data-intensive applications.

Updated on: 2026-03-27T13:20:28+05:30

833 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements