Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Numpy size() function | Python

  • Last Updated : 29 Jul, 2021

In Python, numpy.size() function count the number of elements along a given axis.

Syntax: numpy.size(arr, axis=None)
Parameters: 
arr: [array_like] Input data. 
axis: [int, optional] Axis(x,y,z) along which the elements(rows or columns) are counted. By default, give the total number of elements in a array
Returns: [int] Return the number of elements along a given axis. 
 

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course

Code #1 :  



Python3




# Python program explaining
# numpy.size() method
 
# importing numpy
import numpy as np
 
# Making a random array
arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])
 
# By default, give the total number of elements.
print(np.size(arr))
Output: 
8

 

Code #2 : 

Python3




# Python program explaining
# numpy.size() method
 
# importing numpy
import numpy as np
 
# Making a random array
arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])
 
# count the number of elements along the axis.
# Here rows and columns are being treated
# as elements
 
#gives no. of rows along x-axis
print(np.size(arr, 0))
 
#gives no. of columns along y-axis
print(np.size(arr, 1))
Output: 
2
4

 




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!