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

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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)
chevron_right

Output:



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

Example :

filter_none

edit
close

play_arrow

link
brightness_4
code

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)
chevron_right

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.


Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :