The numpy.size() function in Python returns the number of elements in an array.
It can return either the total count of elements in the entire array or, if an axis is specified, the number of elements along that axis.
This makes it a simple yet powerful utility for understanding array structure, validating data shapes, and iterating over elements in NumPy-based data analysis.
Syntax
numpy.size(arr, axis=None)Parameters:
arr : array_like
Input array whose elements are to be counted.axis : int, optional
Axis along which elements are counted.If not provided (axis=None), returns the total number of elements.
axis=0 → counts rows (along the x-axis).
axis=1 → counts columns (along the y-axis).
Returns:
int — The number of elements in the array or along the specified axis.
Example 1: Total Number of Elements in an Array
# Python program explaining numpy.size() method
import numpy as np
# Creating a 2D NumPy array
arr = np.array([
[1, 2, 3, 4],
[5, 6, 7, 8]
])
# Count total number of elements (default)
print(np.size(arr))
Output:
8
Explanation:
The array has 2 rows × 4 columns = 8 total elements.
Example 2: Counting Elements Along an Axis
# Python program explaining numpy.size() with axis parameter
import numpy as np
arr = np.array([
[1, 2, 3, 4],
[5, 6, 7, 8]
])
# Number of rows (along axis 0)
print(np.size(arr, 0))
# Number of columns (along axis 1)
print(np.size(arr, 1))
Output:
2 4
np.size(arr, 0) → counts the number of rows (2).
np.size(arr, 1) → counts the number of columns (4).
Example 3: Using numpy.size() on Higher-Dimensional Arrays
import numpy as np
# Creating a 3D array
arr = np.arange(24).reshape(2, 3, 4)
print("Array shape:", arr.shape)
# Total number of elements
print("Total elements:", np.size(arr))
# Elements along each axis
print("Axis 0 count:", np.size(arr, 0))
print("Axis 1 count:", np.size(arr, 1))
print("Axis 2 count:", np.size(arr, 2))
Output:
Array shape: (2, 3, 4) Total elements: 24 Axis 0 count: 2 Axis 1 count: 3 Axis 2 count: 4
The array has 2 blocks, each containing a 3×4 matrix.
Hence, total = 2×3×4 = 24 elements.
Example 4: Comparing np.size() vs. np.shape
import numpy as np
arr = np.array([
[1, 2, 3],
[4, 5, 6]
])
print("Array shape:", np.shape(arr))
print("Total elements using np.size:", np.size(arr))
print("Manual calculation:", np.shape(arr)[0] * np.shape(arr)[1])
Output:
Array shape: (2, 3) Total elements using np.size: 6 Manual calculation: 6
The result from np.size() equals the product of dimensions from np.shape().
Example 5: Working with Flattened Arrays
import numpy as np
arr = np.array([[10, 20], [30, 40], [50, 60]])
flat = arr.flatten()
print("Original shape:", arr.shape)
print("Flattened shape:", flat.shape)
print("Total elements in flattened array:", np.size(flat))
Output:
Original shape: (3, 2) Flattened shape: (6,) Total elements in flattened array: 6
Even after flattening, the number of elements remains the same — only the shape changes.
Performance:
numpy.size() runs in O(1) time complexity — it does not iterate through the array. Internally, it simply returns a stored size value from the array’s metadata.
Summary
np.size() returns the total number of elements or counts along a specific axis.
It’s more efficient than manual counting or using loops.
Frequently Asked Questions
Is numpy.size() the same as array.size?
np.size(arr) or arr.size returns the same result — the total number of elements in the array. Can numpy.size() be used with axis on 3D arrays?
axis=0, axis=1, or axis=2 to count elements along each axis in 3D arrays. Does numpy.size() actually iterate through the array?
How is numpy.size() different from len()?
len() returns the size of the first dimension (e.g., number of rows), while np.size() returns the total number of elements or elements along any given axis.