Transpose a NumPy Matrix
Transposing reverses a two-dimensional array's axes so every original row becomes a column.
What is Transpose a NumPy Matrix?
Transposing reverses a two-dimensional array's axes so every original row becomes a column.
Swap matrix rows and columns with NumPy transpose.
When should you use it?
- Perform numerical operations on many values at once.
- Process arrays, matrices, and scientific data efficiently.
- Prepare data for data science and machine learning.
Example code
main.py
import numpy as np
matrix = np.array([[1, 2, 3], [4, 5, 6]])
print(matrix.T)
Expected output
[[1 4]
[2 5]
[3 6]]
How it works
The T attribute returns a transposed view for a two-dimensional array and leaves the original matrix unchanged.
Change the values and run the program in the CodeUtility online Python compiler without installing Python locally.
Practice exercises
Modify the runnable example with the exercises below to build understanding beyond copying the result.
- Change the input shape and dtype.
- Try negative, floating-point, and missing values.
- Apply the operation to larger multidimensional arrays and measure performance.