With no axes argument, np.transpose() simply reverses the order of all axes, which for a 2D array is exactly the same as .T. For arrays with 3 or more dimensions, passing an explicit axes tuple lets you specify precisely how the dimensions should be reordered, rather than just fully reversing them — for example, transposing a stack of images with shape (batch, height, width, channels) into (batch, channels, height, width) requires an explicit axis order, not a simple full reversal.
1Understanding np.transpose()
With no axes argument, np.transpose() simply reverses the order of all axes, which for a 2D array is exactly the same as .T. For arrays with 3 or more dimensions, passing an explicit axes tuple lets you specify precisely how the dimensions should be reordered, rather than just fully reversing them — for example, transposing a stack of images with shape (batch, height, width, channels) into (batch, channels, height, width) requires an explicit axis order, not a simple full reversal.
For anything beyond a plain 2D matrix, use np.transpose(arr, axes=(...)) with an explicit axis order instead of .T — a simple full-axis reversal is rarely the transformation you actually want for 3D+ data.
import numpy as np
matrix = np.array([[1, 2, 3], [4, 5, 6]])
print(np.transpose(matrix))2Practical Example
Here is a real-world application of np.transpose() showing how it is used in production NumPy code.
import numpy as np
images = np.zeros((10, 32, 32, 3))
reordered = np.transpose(images, axes=(0, 3, 1, 2))
print(reordered.shape)3Best Practices
Follow these guidelines when working with np.transpose():
1. Use .T as a shorthand only for simple 2D transposes; use np.transpose() with an explicit axes argument for higher-dimensional arrays
2. Double-check the resulting .shape after a multi-dimensional transpose, since axis-order mistakes are easy to make and hard to spot visually
3. Remember transpose() returns a view, not a copy, so downstream mutations can propagate back to the original array unless you explicitly copy
Tip: For anything beyond a plain 2D matrix, use np.transpose(arr, axes=(...)) with an explicit axis order instead of .T — a simple full-axis reversal is rarely the transformation you actually want for 3D+ data.
import numpy as np
matrix = np.array([[1, 2, 3], [4, 5, 6]])
print(np.transpose(matrix))