NumPy transpose changes the order of an array's axes rather than sorting or reversing its numerical values

An array's dimensions describe how its entries are arranged. NumPy transpose changes the order of those axes. In two dimensions, it gives the familiar matrix transpose: rows become columns and columns become rows. The operation should be explained through positions and shape, rather than described as a numerical calculation.

Academia Master Trader presents this explanation in English for readers in Vanuatu. The examples concern numerical arrays only. They are not prices, trading signals or financial results. A rectangular matrix makes the change of arrangement visible, while a vector shows an important limit of the operation.

Begin with a rectangular matrix

Let a contain rows [1,2,3] and [4,5,6]. Its shape is (2,3). The expression np.transpose(a) produces rows [1,4], [2,5] and [3,6], with shape (3,2).

The first input row becomes the first output column. The second input row becomes the second output column. The value two, originally in the first row and second column, moves to the second row and first column.

Original diagram: a two-by-three matrix with rows one two three and four five six becomes a three-by-two matrix with rows one four, two five and three six after NumPy transpose.

There are six values before and after the operation. The row and column counts exchange places, but no new numerical values are created. Stating the two shapes helps separate a change of arrangement from a change of array size.

A transpose is not a quarter turn

The transposed output begins with [1,4]. A counterclockwise quarter turn of the same matrix would instead begin with [3,6]. Both can produce a three-row arrangement, but their entry positions differ.

A matching output shape therefore does not prove that two operations are equivalent. Follow a specific entry from its original row and column to its new position. That reveals the distinction more clearly than comparing only the dimensions.

Transposition is also not sorting. The output is determined by the axes, regardless of whether the numerical values happen to be increasing, decreasing or mixed. It is the positions that supply the rule.

A one-dimensional vector stays one-dimensional

For the vector [1,2,3,4], np.transpose(a) leaves the vector unchanged. Its shape remains (4,). There is only one axis, so changing the axis order does not turn it into a two-dimensional column.

The reference makes this distinction explicit. To obtain a two-dimensional column from a one-dimensional array, an additional dimension must be introduced. Transposition by itself does not supply that extra axis.

This matters when describing a vector as a row or column. A printed horizontal sequence can still be a one-dimensional array. Read its shape before assuming that transposition will exchange a row dimension with a column dimension.

The default reverses the order of axes

When axes is omitted, transpose reverses the axis order. For a two-dimensional array, (0,1) becomes (1,0). For an input shape (2,3,4,5), the default output shape is (5,4,3,2).

That rule reverses the list of dimensions, not the numerical sequence within every dimension. It should not be confused with flipping entries along an axis. An axis permutation changes which dimension occupies each position in the array description.

For a higher-dimensional input, write the original shape and its reversed order side by side. That makes the default understandable even when a full printed array would contain too many entries to read comfortably.

An explicit permutation names the input axes

The axes argument must contain a permutation of the input axes. The axis at position i in the output corresponds to input axis axes[i]. This is an output-order specification, not a list of new dimension lengths.

For an input with shape (1,2,3), np.transpose(a,(1,0,2)) gives shape (2,1,3). The output first uses input axis one, then input axis zero, then input axis two.

All input axes appear once in that specification. Do not interpret (1,0,2) as a request to create dimensions of lengths one, zero and two. The numbers identify existing axes and state their new order.

Negative axis names count from the end

The documentation allows negative indices in an axis permutation. For an input shape (3,4,5), np.transpose(a,(-1,0,-2)) gives shape (5,3,4).

Here, negative one names the final input axis and negative two names the preceding input axis. The tuple therefore chooses the third, first and second input dimensions in that output order.

The negative signs do not negate any entries and do not request negative dimension lengths. They are a convention for naming axes relative to the end of the input's axis list.

A view is returned whenever possible

The reference states that transpose returns a view whenever possible. Keep that qualification. It is more precise than saying that every possible use always creates an independent copy, or that every use has identical storage behaviour.

A displayed change in layout is not evidence of independent storage. If a later operation needs a separate snapshot of the entries, that requirement should be considered separately from the axis permutation.

When presenting an example, identify the output shape and the axis order first. Discuss storage only with the documented qualification, rather than making a broader promise from the appearance of a printed matrix.

Read positions through the shape

Continue learning with Academia Master Trader at academiamastertrader.com.br. Aprenda provides access to educational material. State the input shape, name the output axis order and follow one entry to its new position. Those details keep transposition distinct from rotation, reversal and adding a dimension.