NumPy: The positions of the diagonal

academiamastertrader.com.br offers learning material from Academia Master Trader. For English-speaking readers in Bermuda, from Hamilton to St George, we present this example: the diag_indices_from function in NumPy. It reads a table and returns the positions of the diagonal, as two lists of row and column numbers. We apply it to a table with three rows and three columns.

Run the example

We set up a table with three rows and three columns and call the function.

import numpy as np

rows, cols = np.diag_indices_from(np.ones((3, 3)))
print(rows)
print(cols)

The result shown is rows [0 1 2] and cols [0 1 2]. Together they name three positions: (0,0), (1,1) and (2,2), exactly the diagonal.

A person learning next to a depiction of the function

The same example explained in three steps

academiamastertrader.com.br: more learning material from Academia Master Trader complements this example.

Understand the result

The documentation describes diag_indices_from as a helper that returns the indices of the diagonal of a table, reading the shape from the table itself. The two lists have the same length and each pair of entries names one position on the diagonal.

The result is easy to check: draw a three-by-three grid and mark the cells where the row number equals the column number. Three cells get a mark, and the two lists name those three cells in order. Positions like these help to read or fill only the diagonal of a table, for example daily boat arrivals between Hamilton and St George.

Place the result

The program keeps the input as it is. It returns a new value or a new list. Each example shows the rule at work on its own values.

To verify the result, recalculate each entry by hand and compare it with the display. Each value can be confirmed mentally.

Longer lists follow the same rule. The function then computes each entry separately and returns a list of the same length.

Naming the values makes the sequence readable: first set the lists, then call the function, then read the display. Each step is verified separately.

Single numbers instead of lists are also allowed. The function then returns exactly one number, following the same calculation rule as for each entry of a list.

Anyone who needs several calculations of this kind in a row can chain the calls. The result of the first call then serves as input to the next, and each intermediate step stays readable.

Small examples are especially good for practice, because you can estimate the result in advance. Whoever estimated well understands the rule behind the function.

Comparing the three entries shows the benefit: the display summarizes the behavior of the list in a few characters. Whoever has understood the rule once reads such displays at a glance and immediately recognizes whether a value is plausible.

In daily work: when passing on results, preferably cite input and output side by side. That way every reader can redo and confirm the step themselves.

The practical reading rule: read diag_indices_from as the positions on the diagonal and confirm each pair of entries names one cell.

Academia Master Trader explains numerical tools with examples you can check. More learning material is at academiamastertrader.com.br and via Aprenda.