NumPy moveaxis relocates selected axes while preserving the order of the others

An array's axes can describe different directions even when its numerical entries are unchanged. NumPy moveaxis relocates selected axes to chosen output positions. Academia Master Trader presents this English lesson for readers in Burkina Faso, using a constructed three-dimensional array to distinguish axis movement from reshaping, sorting and copying.

Moving the first axis to the end

After import numpy as np, define x = np.arange(24).reshape(2,3,4). Its shape is (2,3,4): axis zero has length two, axis one has length three and axis two has length four.

The call moved = np.moveaxis(x,0,-1) produces shape (3,4,2). The original first axis now occupies the last position. The other original axes keep their relative order: the length-three axis still precedes the length-four axis.

A concrete coordinate makes this change visible. The entry x[1,2,3] equals 23, and the corresponding entry in the moved view is moved[2,3,1], also 23.

The coordinate components follow the relocated axes. NumPy has not sorted the values or calculated new measurements.

NumPy moveaxis changes axis positions

Moving the last axis to the front

The call front = np.moveaxis(x,-1,0) produces shape (4,2,3). The original length-four axis becomes the first axis, while the length-two and length-three axes keep their relative order behind it.

For the same source entry, front[3,1,2] equals 23. Its coordinates differ from the previous result because a different axis movement was requested.

Negative positions count from the end. In a three-dimensional input, source position minus one refers to its final axis. Destination position zero means the first axis in the output.

Write down both the input axis labels and the intended output axis labels. The shape alone can be less informative when two axes happen to have equal lengths.

Several axes can move in one call

The call np.moveaxis(x,(0,2),(2,0)) produces shape (4,3,2). Original axis zero goes to output position two, and original axis two goes to output position zero. The remaining axis occupies the middle position.

Source positions must be unique, and destination positions must also be unique. Each selected source axis needs a corresponding destination position.

Think of the source and destination sequences as paired instructions, not as a request to perform unrelated movements one after another on an already changed shape.

Axis relocation returns a view

NumPy documents the result as a view of the input array. A different axis order therefore does not imply independent editable storage.

If later editing must not affect the source, make an explicit copy after the movement, such as independent = np.moveaxis(x,0,-1).copy().

Moving axes is also different from a general reshape. It carries the existing axes and their coordinate meanings to new positions rather than merely requesting another compatible shape.

Continue learning with Academia Master Trader at academiamastertrader.com.br and in Aprenda. Record the source axes, destination positions and one corresponding coordinate before relying on an axis relocation.