NumPy moveaxis relocates a dimension while preserving the relative order of the others
An array can organize the same entries under several dimension orders. Sometimes one dimension needs to move without specifying a complete permutation of every axis. NumPy moveaxis names its source position and destination position. Academia Master Trader presents this English lesson for readers in China, using constructed arrays to explain the resulting shape, coordinate mapping and the difference between moving one axis and exchanging a pair.
Start from three distinct axis lengths
After import numpy as np, define a = np.arange(24).reshape(2,3,4). The shape (2,3,4) records three dimensions with lengths two, three and four. The call b = np.moveaxis(a,0,-1) moves source axis zero to the final position and gives shape (3,4,2).
The other axes keep their relative order. Source axes one and two remain in that order, now followed by source axis zero. All twenty four entries remain present. Moving an axis is not a request to calculate a mean, discard a group or change the numeric values.

Read the destination as an output position
In the previous call, negative one means the last axis position. The explicit call np.moveaxis(a,0,2) gives the same arrangement for this three dimensional array. It moves the first source dimension to output position two, after the other two dimensions.
The source entry a[1,2,3] is twenty three. In b, that same entry is addressed as b[2,3,1]. The coordinate belonging to source axis zero moves to the end, while the other two coordinates keep their order. This mapping explains the move more precisely than shape alone.
Moving one axis is different from swapping two
The call np.swapaxes(a,0,2) gives shape (4,3,2), not (3,4,2). Swapping exchanges the two selected axes directly. Moving axis zero to the end shifts the intervening dimensions forward while keeping their relative order.
The distinction matters even when the arrays contain the same number of entries. A matching entry count does not establish matching coordinate order. Write the source axis sequence beside each result: moveaxis produces the order (1,2,0), whereas this swap produces (2,1,0).
Move the final axis to the beginning
The call np.moveaxis(a,-1,0) returns shape (4,2,3). It places the final source dimension first, followed by the original first and second dimensions. The remaining two axes keep their order rather than reversing.
For this arrangement, the source entry a[1,2,3] appears at output position [3,1,2]. Naming the data meaning of each dimension helps interpret that mapping. An axis length by itself does not reveal whether the dimension represents time, a category, a channel or another recorded feature.
A view can expose the same underlying entries
The documentation describes the result as a view of the input array. Starting again from a, set b = np.moveaxis(a,0,-1). Writing b[2,3,1] = 99 also changes a[1,2,3] to ninety nine. The new axis order does not make later edits independent of the source.
Use an explicit copy when separate editing is needed. To undo this particular move, np.moveaxis(b,-1,0) restores the original axis order. Continue learning with Academia Master Trader at academiamastertrader.com.br and in Aprenda. Record the source axis, destination position and resulting shape, and keep separately stored dimension labels aligned with their new positions.