In this tutorial, you will learn about the NumPy matrix. transpose() in Python. you will learn where to use and how to use the Numpy matrix. transpose.
The ‘NumPy.matrix.transpose() ‘ is a function in Python, that transposes the matrix, which means flips the rows and columns.
Here are the steps on how to use ‘matrix. transpose’ in NumPy:
1. Importing NumPy:
ensure that NumPy is installed and imported into your program.
import numpy as np
2. creating a matrix:
create a matrix using ‘numpy. matrix’.
mat1=np.matrix([[1,2,3],[7,8,9]])
3. Transposing the matrix:
call the ‘transpose()’ to transpose the matrix.
transposed_mat1=mat1.transpose()
4. print the transposed matrix:
use ‘print’ to print the transposed matrix
print(transposed_mat1)
Example:
import numpy as np #create matrix mat1=np.matrix([[1,2,3],[7,8,9]]) #transpose the matrix transposed_mat1=mat1.transpose() #print the transposed matrix print("transposed matrix:" transposed_mat1)
output: transposed matrix:[[1,7] [2,8] [3,9]]