NumPy transpose changes axis order without turning a one dimensional array into a column

An axis describes one dimension of an array. Exchanging axis order changes how positions are addressed, rather than changing the numeric meaning of each entry. NumPy transpose makes that distinction visible in a small matrix and becomes especially useful when an array has several dimensions. Academia Master Trader presents this English lesson for readers in Japan, using constructed examples to separate axis permutation, matrix shape and the addition of a new dimension.

Exchange the two axes of a matrix

After import numpy as np, define m = np.array([[1,4,7],[2,5,8]]). Its shape is (2,3): two rows and three columns. The call np.transpose(m) returns [[1,2],[4,5],[7,8]], with shape (3,2). The first source row becomes the first output column, while the second source row becomes the second output column.

The original entry m[0,2] is seven. After transposing, the corresponding entry is at position [2,0]. The two coordinates exchange roles. All six entries remain present, and none is multiplied, averaged or sorted. The shorter expression m.T gives the same matrix transpose for this two dimensional array.

NumPy transpose

Transposing is different from a quarter turn

The transposed result begins with [1,2]. A counterclockwise quarter turn of this same matrix would begin with [7,8]. Both arrangements have shape (3,2), but their values occupy different positions. A shared shape is not enough to show that two transformations are equivalent.

Follow a known source entry through its coordinates when explaining the operation. For a matrix transpose, exchanging the row and column coordinates gives the destination. This positional account is more precise than saying that the matrix was simply turned, because a turn can imply a different rearrangement.

A single axis has no second axis to exchange

Define v = np.array([3,6,9]). Its shape is (3,), meaning it has one axis. The call np.transpose(v) still has shape (3,) and the same three entries. Writing v.T does not create a two dimensional column matrix either.

To form a column explicitly, use v[:,np.newaxis]. The result is [[3],[6],[9]], with shape (3,1). That expression adds an axis. Adding a dimension and reordering existing dimensions are separate operations, even if printed outputs sometimes make them look similar.

Specify where each source axis goes

For a three dimensional example, define a = np.arange(24).reshape(2,3,4). The call np.transpose(a,axes=(1,0,2)) returns an array with shape (3,2,4). Output axis zero corresponds to source axis one, output axis one corresponds to source axis zero, and output axis two still corresponds to source axis two.

Without an axes argument, transpose reverses the complete axis order. For a, that default gives shape (4,3,2). An explicit axis order must be a permutation of the existing axes: each source axis appears once. It is not a list of selected entries or a request to discard a dimension.

Track shared data as well as shape

The documentation states that a view is returned whenever possible. For the ordinary matrix in this lesson, the transposed arrangement can share data with its source. Starting again from m, set t = np.transpose(m). Writing t[2,0] = 99 also changes m[0,2] to ninety nine.

Use an explicit copy when independent editing is needed. Keep separately stored row and column labels aligned with their new roles. Continue learning with Academia Master Trader at academiamastertrader.com.br and in Aprenda. Record the source shape, requested axis order and output shape to make the positional change clear.