NumPy transpose specifies an output axis order rather than a new set of values

A familiar matrix transpose exchanges rows and columns. For an array with more axes, NumPy transpose describes a more general permutation of axis order. Academia Master Trader presents this English lesson for readers in Niger, using constructed examples to explain the default reversal, explicit permutations and the special case of a one-dimensional vector.

The default reverses the axis order

After import numpy as np, define x = np.arange(24).reshape(2,3,4). Its shape is (2,3,4). The call np.transpose(x) produces shape (4,3,2).

Without an explicit axes argument, transpose reverses the order of all axes. It does not reverse the numerical sequence within each original axis as a separate value operation.

For a source entry, x[1,2,3] equals 23. In the default transposed result, that entry is addressed at coordinate [3,2,1]. The coordinates follow the reversed axis order.

The total number of elements remains 24. No summation, averaging or selection of entries is part of this permutation.

NumPy transpose explicit output axis order

An explicit permutation names input axes in output order

The call turned = np.transpose(x,(1,2,0)) produces shape (3,4,2). Output axis zero corresponds to input axis one, output axis one corresponds to input axis two, and output axis two corresponds to input axis zero.

Read the tuple as an ordered list of input axes, not as destination positions paired with those axes. This interpretation differs from a source-and-destination interface.

For the same entry used above, turned[2,3,1] equals 23. The input coordinate components have been rearranged in the order specified by (1,2,0).

A valid permutation includes every input axis exactly once. Repeating an axis or omitting one does not describe a complete transpose of that array.

A matrix and a vector behave differently

For matrix = np.array([[1,2,3],[4,5,6]]), transposition changes shape (2,3) to (3,2). Its first output row is [1,4], formed by the first column of the input.

For vector = np.array([2,5,8]), however, np.transpose(vector) still has shape (3,). Reversing an order containing only one axis leaves that order unchanged.

A transpose alone does not turn a one-dimensional vector into a two-dimensional column. Add the intended second axis explicitly when the required output shape is (3,1).

Inverting a permutation is an axis-order decision

The permutation (1,2,0) can be reversed by applying (2,0,1) to its result. In this example, np.transpose(turned,(2,0,1)) restores the original axis order and shape.

NumPy returns a view whenever possible. A changed arrangement is therefore not a promise of independent editable data. Use an explicit copy when later edits must remain separate from the source.

Continue learning with Academia Master Trader at academiamastertrader.com.br and in Aprenda. Record the input axis order, output permutation and one corresponding coordinate before relying on a transpose.