NumPy diagonal reads entries at linked row and column positions without adding them
A diagonal is a relationship between positions inside an array, not a judgement about which numbers matter most. Its entries can be read in order without sorting them or combining them into a total. Academia Master Trader explains NumPy diagonal in English for readers in Slovenia. A small square matrix lets us follow the main diagonal and compare positive and negative offsets. These examples describe data organisation. They do not describe financial outcomes or extra observations beyond the supplied integers. Each result will be checked against explicit coordinates.
Record the matrix before reading a diagonal
After import numpy as np, define a = np.array([[8,3,6],[2,9,4],[7,5,1]]). Its shape is (3,3), with three rows and three columns. Both row and column indices run from zero through two. The value eight occupies (0,0), nine occupies (1,1) and one occupies (2,2). Those coordinates share the same row and column index within each pair.
The call np.diagonal(a) returns [8,9,1], with shape (3,). It follows the main diagonal from the upper left towards the lower right. The output is one dimensional because it records an ordered sequence of diagonal entries. It is not a smaller square matrix with the other entries filled with zeros. The original matrix is the source of the selected values.
Separate selection from addition
Reading [8,9,1] tells us which entries lie on the main diagonal and in which order they appear. It does not calculate their sum. Although those three integers could be added in a separate step, that would be a different operation. NumPy's documentation lists a separate function for summing diagonal entries. Keeping the operation names separate makes the intermediate structure easier to explain.
The numerical sizes of the entries do not determine whether they belong to the diagonal. Nine happens to be the largest supplied value, but eight and one are selected for the same positional reason. Six, at (0,2), is not on the main diagonal even though it is larger than one. The condition here concerns coordinates, not a numerical cutoff.

Read the highlighted path in the figure
The figure highlights eight, nine and one inside the input matrix, then records the corresponding one dimensional output. Other input cells remain visible for context. Read the highlighted cells by increasing row index. This gives eight first, nine second and one third. The display does not show a time series or a price path; its positions refer only to this example matrix.
Follow nine in particular. Its input coordinate is (1,1), while its output index is one. A pair of coordinates in a matrix has become one position in the diagonal sequence. That change explains the output shape without requiring any change to the numerical value. The same reasoning applies to eight and one at the ends of the sequence.
Move above or below the main diagonal
The call np.diagonal(a,offset=1) returns [3,4], with shape (2,). It reads positions (0,1) and (1,2). The column index is one greater than the row index, placing these entries above the main diagonal. The output is shorter because only two such coordinate pairs fit within this matrix. The function does not invent a third entry to match the main diagonal length.
The call np.diagonal(a,offset=-1) returns [2,5], also with shape (2,). It reads positions (1,0) and (2,1), below the main diagonal. A negative offset does not mean taking negative numerical values or subtracting one from each entry. It selects a different diagonal according to the relationship between row and column positions. The entries themselves stay two and five.
Keep the scope of the example clear
A rectangular matrix can also have a diagonal. For b = np.array([[8,3,6],[2,9,4]]), whose shape is (2,3), np.diagonal(b) returns [8,9]. The main diagonal stops when no further matching row and column position is available. It does not require the input to be square, and it does not reuse the final row to reach the third column.
Continue learning with Academia Master Trader at academiamastertrader.com.br and in Aprenda. Record the input shape and offset, list the selected coordinate pairs and only then write the output sequence. This keeps a diagonal selection distinct from a sum, a ranking or a rearrangement of every matrix entry.