What is Array Transposition?
By transposing an array in NumPy, we mean rearranging its dimensions to access its data along different axes:
- For a 2D Array (Matrix): Transposing flips the matrix across its main diagonal, effectively swapping rows and columns. An array with shape
(m, n)becomes(n, m), where the element at(i, j)moves to(j, i). - For Multidimensional Arrays (≥ 3D): Transposing involves permuting the order of the axes according to a specified sequence.
NumPy returns a view of the original array whenever possible without copying data, making transpositions virtually instantaneous.
1. Transposing Arrays Using numpy.transpose()
The transpose() function in NumPy rearranges the dimensions of an array. If the order of axes is not specified, NumPy reverses the dimensions completely.
Syntax
| Parameter | Description |
|---|---|
a |
The input array-like object to be transposed. |
axes (optional) |
Tuple or list of ints specifying the new order of axes. If omitted (or None), it defaults to completely reversing the dimensions (e.g. (0, 1, 2) → (2, 1, 0)). |
Example 1: Transposing a 2D Array (Rows ↔ Columns)
Here, a (2, 3) array is transposed into a (3, 2) array, converting rows into columns:
Output:
Visual Breakdown
2. Transposing 3D Arrays
When transposing a 3D array without specifying axes, NumPy reverses all dimensions: axis 0 becomes axis 2, and axis 2 becomes axis 0.
Example: Transposing a 3D Array with Default Reversal
Output:
3. Transposing with Specified Axes (Permutation)
In machine learning and computer vision (e.g. converting PyTorch (Channels, Height, Width) to TensorFlow (Height, Width, Channels)), you often need to permute specific axes while leaving others untouched.
You can achieve this by passing a tuple of axis indices to the axes parameter.
Example: Swapping Axes 1 and 2 While Keeping Axis 0
In this example, we keep the first dimension (axis 0) unchanged and swap axes 1 and 2 by setting axes=(0, 2, 1):
Output:
4. Transposing Arrays Using the .T Attribute
NumPy arrays provide a convenient shorthand attribute: ndarray.T. It provides an instant way to transpose arrays without writing np.transpose().
Example: Quick Transposition via .T
Output:
Common NumPy Gotcha: 1D Arrays and .T
Calling .T on a 1-dimensional array does not change its shape:
print(a.shape) # (3,)
print(a.T.shape) # (3,) — Still 1D!
Because a 1D array has only one axis, reversing (0,) still gives (0,). If you need a 2D column vector, use a.reshape(-1, 1) or a[:, np.newaxis].
5. Summary: np.transpose() vs .T
| Feature | np.transpose(arr, axes) |
arr.T |
|---|---|---|
| Type | NumPy module function | ndarray property attribute |
| Axis Reordering | Supports arbitrary custom axis permutation via axes=(...) |
Always reverses all axes |
| Memory View | Returns a view (no memory allocation) | Returns a view (no memory allocation) |
| Best For | Complex multidimensional tensor axis swapping | Quick 2D matrix transposition |
Array Transposition Quiz
Test your understanding of array transposition in NumPy. Click an option to see instant feedback.