Last Updated : 12 Jul, 2025
numpy.size()
function in Python is used to count the number of elements in a NumPy array. You can use it to get the total count of all elements, or to count elements along a specific axis, such as rows or columns in a multidimensional array. This makes it useful when quickly trying to understand the shape or structure of the given data.
Syntax:
numpy.size(arr, axis=None)
Where:
Here we create a 2D array arr with 2 rows and 4 columns and use np.size() function which will return the total number of elements in the array.
Python
import numpy as np
arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])
print(np.size(arr))
Output:
2. To Count the Elements Along a Specific Axis8
Here 0 is used to denote the axis as rows and 1 is used to denote axis as columns. Therefore np.size(arr, 0) will returns the number of rows and np.size(arr, 1) returns the number of columns.
Python
import numpy as np
arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])
print(np.size(arr, 0))
print(np.size(arr, 1))
Output:
3. To Count Elements in a 3D Array2
4
In this case we are working with a 3D array having the shape (2, 2, 2)
. Here:
axis=0
refers to the number of blocks (first level of depth).axis=1
refers to the number of rows in each block.axis=2
refers to the number of columns in each row.
import numpy as np
arr = np.array([[[1, 2], [3, 4]],
[[5, 6], [7, 8]]])
print(np.size(arr))
print(np.size(arr, 0))
print(np.size(arr, 1))
print(np.size(arr, 2))
Output:
8
2
2
2
The numpy.size()
function is a tool to understand how many elements exist in your array whether it's one-dimensional or multi-dimensional. It's helpful when you're working with large datasets and want to inspect structure or dimensions.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4