Numpy matrix.transpose() in Python
matrix.transpose() method in NumPy is used to find the transpose of a matrix that is, it flips the matrix over its diagonal, turning rows into columns and columns into rows.
Example:
Input: [[1,2]
[3,4]]Output: [[1,3]
[2,4]]
Syntax
matrix.transpose()
Returns: A new matrix that is the transposed version of the original.
Example 1: This creates a 2×3 matrix and finds its transpose using the transpose() method.
import numpy as np
a = np.matrix([[1, 2, 3], [4, 5, 6]])
b = a.transpose()
print("Transposed Matrix:")
print(b)
Output
Transposed Matrix: [[1 4] [2 5] [3 6]]
Example 2: Here, a 3×3 matrix is created and transposed using the same method.
import numpy as np
a = np.matrix('[4, 1, 9; 12, 3, 1; 4, 5, 6]')
b= a.transpose()
print(b)
Output
[[ 4 12 4] [ 1 3 5] [ 9 1 6]]
Example 3: Transpose in Matrix Multiplication
import numpy as np
a = np.matrix([[1, 2], [3, 4]])
b = np.matrix([[5, 6], [7, 8]])
res = a* b.transpose()
print(res)
Output
[[17 23] [39 53]]