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

1

2

3

4

5

6

7

8

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

1

2

3

4

5

6

7

8

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.

1

2

3

4

5

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 )

1

2

3

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

1

print("Size of arr_2D = ", arr_2D.size)

1

Output >>> Size of arr_2D =  9