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:
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.