Skip to content
Related Articles

Related Articles

Find the number of rows and columns of a given matrix using NumPy
  • Last Updated : 29 Aug, 2020
GeeksforGeeks - Summer Carnival Banner

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)

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.

My Personal Notes arrow_drop_up
Recommended Articles
Page :