🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEnumpy

numpy Documentation

LOADING ENGINE...

np.transpose()

AI & DATA SCIENCE // np-transpose

np.transpose() reverses or permutes the axes of an array, generalizing the simple row/column swap of .T to arrays with any number of dimensions and a custom axis order.

Syntax

np.transpose(arr, axes=None)

Deep Dive Course

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.

editor.html
import numpy as np

matrix = np.array([[1, 2, 3], [4, 5, 6]])
print(np.transpose(matrix))
localhost:3000

2Practical Example

Here is a real-world application of np.transpose() showing how it is used in production NumPy code.

editor.html
import numpy as np

images = np.zeros((10, 32, 32, 3))
reordered = np.transpose(images, axes=(0, 3, 1, 2))
print(reordered.shape)
localhost:3000

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.

editor.html
import numpy as np

matrix = np.array([[1, 2, 3], [4, 5, 6]])
print(np.transpose(matrix))
localhost:3000

Examples

Example 01Basic Usage
import numpy as np

matrix = np.array([[1, 2, 3], [4, 5, 6]])
print(np.transpose(matrix))
Example 02Advanced Example
import numpy as np

images = np.zeros((10, 32, 32, 3))
reordered = np.transpose(images, axes=(0, 3, 1, 2))
print(reordered.shape)

Best Practices

  • Use .T as a shorthand only for simple 2D transposes; use np.transpose() with an explicit axes argument for higher-dimensional arrays
  • Double-check the resulting .shape after a multi-dimensional transpose, since axis-order mistakes are easy to make and hard to spot visually
  • Remember transpose() returns a view, not a copy, so downstream mutations can propagate back to the original array unless you explicitly copy

Interview Question

For a 3D array, why can't you just rely on the default (no axes argument) behavior of np.transpose() the way you can for a 2D array?

Hint: Think about what 'reversing all axes' means once there are more than two of them.

For a 2D array, reversing the axis order and swapping rows/columns are the same operation, so the default behavior happens to match what people usually mean by 'transpose'. For 3 or more dimensions, fully reversing the axis order is just one specific permutation among many possible ones, and it's rarely the reordering you actually want, like moving a channels dimension from last to second, so higher-dimensional code needs to pass an explicit axes tuple describing exactly which dimension goes where, rather than relying on a full reversal.

Exercises

MediumPractice using np.transpose() in a real scenario.
View Solution
import numpy as np

matrix = np.array([[1, 2, 3], [4, 5, 6]])
print(np.transpose(matrix))

Frequently Asked Questions

For a 3D array, why can't you just rely on the default (no axes argument) behavior of np.transpose() the way you can for a 2D array?

For a 2D array, reversing the axis order and swapping rows/columns are the same operation, so the default behavior happens to match what people usually mean by 'transpose'. For 3 or more dimensions, fully reversing the axis order is just one specific permutation among many possible ones, and it's rarely the reordering you actually want, like moving a channels dimension from last to second, so higher-dimensional code needs to pass an explicit axes tuple describing exactly which dimension goes where, rather than relying on a full reversal.

Related Functions

ndarray-tnp-swapaxesnp-reshape