Let’s learn how to transpose matrix in Numpy Python library.
Using a numpy transpose method
I’ll create 5 x 2 array and transpose it into a new one. You will see how easy is that with Numpy.
Of course Numpy offers dedicated function for that purpose.
import numpy as np my_array = np.arange(11, 21).reshape(5, 2) transposed_array = np.transpose(my_array) print(my_array) print(transposed_array)
I created 5 x 2 array with values 11 – 20. You can see how values got transposed in the picure above.
The transpose function returns a new matrix that is the transpose of the original matrix. In the example above, the original matrix has five rows and two columns, but the transposed matrix has two rows and five columns.