NumPy swapaxes exchanges two axis positions without sorting the stored values
Array shape describes the lengths of axes in a particular order. NumPy swapaxes exchanges two of those axis positions. Academia Master Trader presents this English lesson for readers in Mali, using a constructed array to show how coordinates follow the exchange and why equal axis lengths can hide a structural change.
Exchange the first and final axes
After import numpy as np, define x = np.arange(24).reshape(2,3,4). Its shape is (2,3,4), and the call swapped = np.swapaxes(x,0,2) produces shape (4,3,2).
The original first axis becomes the final axis, while the original final axis becomes the first axis. The middle axis stays in the middle. No axis is removed, and no new singleton axis is inserted.
For a concrete coordinate, x[1,2,3] equals 23. In the swapped view, the corresponding entry is swapped[3,2,1], also 23. The first and last coordinate components exchange places alongside their axes.
This operation does not rank, sort or aggregate the entries. It changes their axis-based interpretation.

A row-shaped array can become column-shaped
For row = np.array([[2,5,8]]), the input shape is (1,3). The call np.swapaxes(row,0,1) produces shape (3,1) and displays [[2],[5],[8]].
This two-dimensional example exchanges the row and column axes. Its three values remain present, with the same correspondence between source entries and output coordinates.
A one-dimensional vector is different. An input with shape (3,) has only one axis, so there are not two distinct axes available to exchange. Add the intended structure explicitly before asking for an exchange that requires two axes.
Equal lengths can conceal the exchange
Consider cube = np.arange(8).reshape(2,2,2). Swapping axes zero and two leaves the shape (2,2,2), because the exchanged axes have the same length.
The coordinate correspondence still changes. The source entry cube[0,0,1] equals 1, while its corresponding entry after the exchange is at output coordinate [1,0,0].
An unchanged shape therefore does not prove an unchanged axis arrangement. Use axis names or a specific coordinate to explain the intended exchange when axis lengths are equal.
A view is not independent editable storage
For an ndarray input in current NumPy, swapaxes returns a view. Do not assume that changing the arrangement creates a separate copy of its values.
If later editing must remain independent of the source, use an explicit copy, such as independent = np.swapaxes(x,0,2).copy().
Applying the same exchange twice restores the original axis order. That reversibility concerns the arrangement of axes, not a promise that intermediate arrays own separate data.
Continue learning with Academia Master Trader at academiamastertrader.com.br and in Aprenda. Record the two exchanged axes, output shape and a corresponding coordinate before relying on the rearranged array.