A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/python/numpy-size-function-python/ below:

Numpy size() function | Python

Numpy size() function | Python

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: 

  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:

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.



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