NumPy swapaxes exchanges two array dimensions while keeping every entry accounted for

A numeric array can describe several dimensions at once. Moving two of those dimensions changes the coordinate order used to find each entry, rather than calculating new numeric values. NumPy swapaxes names the two axes to exchange. Academia Master Trader presents this English lesson for readers in South Korea, using constructed arrays to connect axis numbers, output shape and the source position of a relocated entry.

Begin with a row that already has two dimensions

After import numpy as np, define r = np.array([[3,6,9]]). The doubled brackets create a two dimensional array with shape (1,3). The call np.swapaxes(r,0,1) returns [[3],[6],[9]], with shape (3,1). The row axis and column axis exchange positions.

All three entries remain present. The output has three rows and one column instead of one row and three columns. The operation does not divide each value, sort the values or insert extra observations. It changes which coordinate describes the row and which describes the column.

NumPy swapaxes

Axis numbers are not entry positions

The arguments zero and one refer to dimensions, not to the first two numeric entries. In the previous example, swapping axes does not mean exchanging three with six while leaving nine where it was. It exchanges the roles of the two dimensions of the complete array.

A one dimensional array such as np.array([3,6,9]) has only axis zero. It does not have an existing axis one to exchange with axis zero. If the intended result needs an additional dimension, create that dimension explicitly before choosing which axes to exchange.

Follow a three dimensional shape

Define a = np.arange(24).reshape(2,3,4). Its three axis lengths are two, three and four. The call s = np.swapaxes(a,0,2) gives an array with shape (4,3,2). Axis zero and axis two exchange positions, while axis one keeps its place in the order.

This example contains twenty four entries before and after the exchange. It does not become a smaller sample or a summary. A change from two leading groups to four leading groups describes a different arrangement of dimensions, not an increase in the amount of recorded information.

Trace coordinates through the exchange

The source entry a[1,2,3] is twenty three. After exchanging axes zero and two, the same entry is at s[3,2,1]. The outer coordinates exchange roles; the middle coordinate remains two. Following the coordinate mapping explains the movement more clearly than relying only on a printed block of numbers.

The call np.swapaxes(s,0,2) restores the original arrangement. Exchanging the same pair twice returns the dimensions to their starting order. This is an arrangement check, not a claim that independently stored labels or other records have also been moved.

Shared storage matters during later editing

For a NumPy ndarray in current versions, the documented result is a view. Starting again from a, set s = np.swapaxes(a,0,2). Writing s[3,2,1] = 99 also changes a[1,2,3] to ninety nine. Two coordinate arrangements can address the same underlying data.

Use an explicit copy when later changes must stay separate. Give each source axis a clear meaning before exchanging it, and carry that meaning into the output labels. Continue learning with Academia Master Trader at academiamastertrader.com.br and in Aprenda. Record the exchanged pair, original shape and resulting shape so another reader can trace each dimension without guessing.