Array axes carry the row, column, batch, channel, or feature meaning that later calculations expect. Transposing a NumPy array changes that axis order so data loaded in one orientation can be used by matrix products, plotting routines, or shape-sensitive preprocessing code.
For a two-dimensional array, .T is the short form of transpose() and turns a (rows, columns) matrix into (columns, rows). Higher-dimensional arrays need an explicit axis tuple when reversing every axis is not the intended layout.
Most transpose results are views when NumPy can describe the new order with strides instead of copying values. Check memory sharing before writing through a transposed result, and add a new axis for one-dimensional arrays when code needs a column vector because vector.T stays one-dimensional.
Related: Multiply matrices
Related: Reshape an array
Related: Check view or copy state
Steps to transpose a NumPy array:
- Create a script that transposes a matrix and swaps two axes in a three-dimensional array.
- array-transpose.py
import numpy as np matrix = np.array( [ [1, 2, 3], [4, 5, 6], ] ) columns = matrix.T cube = np.arange(24).reshape(2, 3, 4) swapped = np.transpose(cube, axes=(1, 0, 2)) vector = np.array([1, 2, 3]) column_vector = vector[:, np.newaxis] print("matrix shape:", matrix.shape) print("matrix.T shape:", columns.shape) print(columns) print("cube shape:", cube.shape) print("swapped shape:", swapped.shape) print("axis value check:", cube[1, 2, 3] == swapped[2, 1, 3]) print("transpose shares memory:", np.shares_memory(matrix, columns)) print("vector.T shape:", vector.T.shape) print("column vector shape:", column_vector.shape)
Use .T for the common two-dimensional transpose. Use np.transpose(…, axes=…) when each output axis must map to a specific input axis.
- Run the script.
$ python array-transpose.py matrix shape: (2, 3) matrix.T shape: (3, 2) [[1 4] [2 5] [3 6]] cube shape: (2, 3, 4) swapped shape: (3, 2, 4) axis value check: True transpose shares memory: True vector.T shape: (3,) column vector shape: (3, 1)
- Check the matrix transpose shape and values.
The (2, 3) matrix becomes (3, 2), and each printed row from matrix.T contains one original column.
- Confirm the three-dimensional axis order.
axes=(1, 0, 2) swaps the first two axes and keeps the last axis in place, so the shape changes from (2, 3, 4) to (3, 2, 4). The axis value check line proves the same value moved to the expected position.
- Check whether the transposed result shares memory with the source.
transpose shares memory: True means columns is a view of matrix in this example. Use columns.copy() before in-place edits when the original matrix must stay unchanged.
Related: Check view or copy state - Use a new axis when a one-dimensional vector must become a column vector.
vector.T shape: (3,) shows that transposing a one-dimensional array does not make it two-dimensional. vector[:, np.newaxis] creates the (3, 1) column vector shape.
- Remove the test script if it was only used to confirm behavior.
$ rm array-transpose.py
Mohd Shakir Zakaria is a cloud architect with deep roots in software development and open-source advocacy. Certified in AWS, Red Hat, VMware, ITIL, and Linux, he specializes in designing and managing robust cloud and on-premises infrastructures.