NumPy array size – np.size() | Python NumPy Tutorial

To find python NumPy array size use size() function. The NumPy size() function has two arguments. First is an array, required an argument need to give array or array name. Second is an axis, default an argument. The axis contains none value, according to the requirement you can change it.

The np.size() function count items from a given array and give output in the form of a number as size. 

Syntax: np.size(array, axis=None)

NumPy Array Size

import numpy as np # import numpy package

arr_2D = np.array([[0, 1, 1], [1, 0, 1], [1, 1, 0]]) # create NumPy 2D array
print("2D array : \n", arr_2D) # print array

array_size = np.size(arr_2D) # find the size of arr-2D

print('Size of arr_2D = ', array_size) # print size of array
Output >>>

2D array : 
 [[0 1 1]
 [1 0 1]
 [1 1 0]]

Size of arr_2D =  9

If you want to count how many items in a row or a column of NumPy array. Then give the axis argument as 0 or 1.

items_in_col = np.size(arr_2D, axis = 0) # give columns 
print("No. of items in a column = ", items_in_col )

items_in_row = np.size(arr_2D, axis = 1)
print("No. of items in a row = ", items_in_row )
Output >>> 
No. of items in a column =  3
No. of items in a row =  3

You can find the size of the NumPy array using size attribute.

Syntax: array.size

print("Size of arr_2D = ", arr_2D.size)
Output >>> Size of arr_2D =  9

To learn more about python NumPy library click on the bellow button.

Leave a Reply

Top