Skip to content
Related Articles

Related Articles

Find the number of rows and columns of a given matrix using NumPy

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 29 Aug, 2020

In the NumPy with the help of shape() function, we can find the number of rows and columns. In this function, we pass a matrix and it will return row and column number of the matrix.

Syntax:

shape()

Return: The number of rows and columns.

Example:

Python




import numpy as np
  
  
matrix= np.arange(1,9).reshape((3, 3))
  
# Original matrix
print(matrix)
  
# Number of rows and columns of the said matrix
print(matrix.shape)

Output:

[[1 2 3]
[4 5 6]
[7 8 9]]
(3,3)

Example :

Python




import numpy as np
  
  
matrix= np.arange(10,15).reshape((3, 2))
  
# Original matrix:
print(matrix)
  
# Number of rows and columns of the said matrix
print(matrix.shape)

Output

[[10 11]
[12 13]
[14 15]]
(3,2)
My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!