Numpy size () function | python

NumPy | Python Methods and Functions

In Python, numpy.size () counts the number of items along a given axis.

Synatx: numpy.size (arr, axis = None)

Parameters:
arr: [array_like] Input data.
axis: [int, optional] Axis along which the elements are counted. By default, give the total number of elements.

Returns: [int] Return the number of elements along a given axis.

Code # 1:

# Python program explaining
# numpy.size () method

 
# numpy imports

import numpy as geek 

  
# Generate a random array

arr = geek.array ([[ 1 , 2 , 3 , 4 ], [ 5 , 6 , 7 , 8 ]])

 
# By default, specify the total number of items.

print (geek.size (arr))

Output:

 8 

Code # 2:

# Python program explaining
# numpy.size () method

 
# numpy import

import numpy as geek 

 
# Generate a random array

arr = geek.array ([[ 1 , 2 , 3 , 4 ], [ 5 , 6 , 7 , 8 ]] )

 
# count the number of items along the axis.

print (geek.size (arr, 0 ))

print   (geek.size (arr, 1 ))

Exit:

 2 4 




Get Solution for free from DataCamp guru