Open In App

Numpy size() function | Python

Last Updated : 15 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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: 

  1. arr is input data in the form of an array.
  2. axis represent along which the elements (rows or columns) are counted.
  3. The function returns an integer as an output representing the number of elements. 

Example Usages of numpy.size() Function

1. To Find Total Number of Elements

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:

8

2. To Count the Elements Along a Specific Axis

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:

2
4

3. To Count Elements in a 3D Array

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.
Python
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.



Next Article
Article Tags :
Practice Tags :

Similar Reads