NumPy diagonal extracts diagonal entries and appends a diagonal axis

A diagonal can be useful as a sequence before any total is calculated. NumPy diagonal returns entries from a selected diagonal while keeping them available individually. Academia Master Trader presents this English lesson for readers in Madagascar, using constructed arrays to explain offsets, axis selection and the shape of the returned diagonal array.

A matrix diagonal becomes a one-dimensional sequence

After import numpy as np, define m = np.array([[5,7,9],[10,12,14],[15,17,19]]). The call np.diagonal(m) returns [5,12,19].

These are the entries at positions (0,0), (1,1) and (2,2). Their order follows the selected diagonal from its beginning towards increasing indices. The result has shape (3,), rather than the matrix shape (3,3).

Unlike trace, diagonal does not add the selected values. Unlike an upper triangular selection, it does not preserve the full matrix with excluded positions replaced by zero. Its output is the diagonal sequence itself.

NumPy diagonal extraction

An offset selects a parallel diagonal

The call np.diagonal(m,offset=1) returns [7,14]. A positive offset selects a diagonal above the main diagonal. The first selected position is row zero, column one.

The call np.diagonal(m,offset=-1) returns [10,17]. A negative offset selects a diagonal below the main diagonal, beginning at row one, column zero in this example.

Neither call moves entries within the matrix. Offset changes which coordinates are read. The two selected diagonals have length two, so both outputs have shape (2,).

Rectangular matrices also have diagonals. Their lengths depend on the matrix dimensions and the selected offset, rather than on an assumption that every diagonal has the same length.

The diagonal axis goes at the end

For arrays with more than two dimensions, axis1 and axis2 identify the matrix plane. Their defaults are zero and one. The remaining axes keep their relative order, and the diagonal axis is appended at the end.

Define layers = np.arange(8).reshape(2,2,2). The call np.diagonal(layers) returns [[0,6],[1,7]]. In contrast, np.diagonal(layers,axis1=1,axis2=2) returns [[0,3],[4,7]].

Both results have shape (2,2), yet their entries differ because the matrix planes differ. Shape alone cannot establish that the same diagonal selection was made. Record the meanings of both selected axes and the remaining axis.

Make an explicit copy before independent editing

The current NumPy documentation describes diagonal as returning a read-only view of the original array. It also describes historical and possible future changes to write behavior. Do not depend on editing that returned view to behave identically across versions.

Use d = np.diagonal(m).copy() when an independent, editable array is needed. For example, assigning d[0] = 99 then changes the copy without changing m. This separates diagonal extraction from a later decision to modify values.

Continue learning with Academia Master Trader at academiamastertrader.com.br and in Aprenda. Keep the offset, selected axes, diagonal length and copying decision beside the extracted sequence.