NumPy diagonal selects a diagonal of entries rather than adding them or building a new matrix

A matrix contains several useful paths through its entries. A row follows one direction, a column follows another, and a diagonal advances along both selected axes at once. NumPy diagonal returns the entries on a specified diagonal. It does not add them together or automatically turn them into a new square matrix.

Academia Master Trader offers this English-language lesson for readers in Ireland. The numbers are invented to explain array positions, offsets and shapes. They are not market prices, trading signals or financial results. The aim is to connect each returned value to a clear position in the original array before giving the output any further meaning.

Reading the main diagonal

Consider a matrix a whose rows are [2,5,8], [11,14,17] and [20,23,26]. The expression np.diagonal(a) returns [2,14,26]. These values occupy positions (0,0), (1,1) and (2,2). The row index and the column index increase together along the main diagonal.

The input shape is (3,3), while the output shape is (3,). The result contains three selected values, not all nine entries. The values themselves are unchanged. Fourteen is returned because of its position in the matrix, not because it was calculated from the values around it.

Original diagram showing NumPy diagonal selecting 2, 14 and 26 from a three by three matrix

Keeping selection separate from addition

For this example, adding the selected values would give 42. That is a different operation from returning [2,14,26]. The documentation lists trace as a related function that sums along diagonals. A reader should therefore be told whether the intended result is a sequence of entries or one combined number.

This distinction also changes the shape of the output. A one-dimensional array with three values preserves the individual diagonal entries. A sum removes that detail. If later work needs the entries separately, replacing selection with addition changes the information available for that work.

Moving above the main diagonal

The offset argument chooses a diagonal relative to the main one. With np.diagonal(a,offset=1), the result is [5,17]. These values occupy positions (0,1) and (1,2). For this two-dimensional array, the positive offset moves the selected path above the main diagonal.

The output is shorter because this path reaches the edge after two entries. Using offset=2 gives [8], the single entry at position (0,2). The offset is a positional instruction. It is not a value added to the entries and does not mean that every output should have the same length.

Moving below the main diagonal

With np.diagonal(a,offset=-1), the result is [11,23]. The selected positions are (1,0) and (2,1). A negative offset selects below the main diagonal for the usual row and column axes. With offset=-2, only the entry 20 remains on the chosen path.

The sign changes which path is selected, not the signs of the stored numbers. Eleven does not become negative when the offset is negative. Writing down the selected positions beside the values is a simple way to keep those two meanings separate.

Rectangular arrays also have diagonals

A square input is not required. Suppose b has the rows [2,5,8] and [11,14,17]. Its shape is (2,3). The expression np.diagonal(b) returns [2,14], because the main diagonal cannot continue after the second row. The output shape is (2,).

For the same b, offset=1 returns [5,17]. An offset of -1 returns [11]. The available row and column positions jointly determine the length. A diagonal does not invent another row to match the number of columns, nor does it fill missing positions with zeros.

Naming the axes for higher-dimensional input

The arguments axis1 and axis2 identify the two axes used to form the plane from which diagonals are taken. Their default values are 0 and 1. When the input has more than two dimensions, leaving these defaults in place may select a different plane from the one a reader expects.

The documentation explains the output shape by removing those two selected axes and appending a new final axis for the diagonal entries. Other dimensions remain. For example, an array with shape (4,3,3), using axis1=1 and axis2=2 with the main diagonal, has output shape (4,3). Four separate three by three planes each contribute three entries.

Not confusing the main and opposite diagonals

In the original three by three example, the path from the upper-right corner to the lower-left corner contains [8,14,20]. That is not what np.diagonal(a) returns by default. The default path begins at the upper-left corner and moves through [2,14,26].

The documentation shows that reversing a matrix horizontally or vertically before selecting a diagonal can produce the opposite diagonal, with the order depending on the reversal used. It is therefore useful to describe the starting corner and reading direction rather than calling every sloping path the same diagonal.

Making independent storage explicit

The current documentation describes a read-only view for the returned diagonal and discusses behaviour across NumPy versions. If you need an independent array to edit, use np.diagonal(a).copy() explicitly. This avoids relying on the returned selection being a separate writable copy of the data.

Continue learning with Academia Master Trader at academiamastertrader.com.br. Aprenda provides educational content. Record the input shape, mark the offset and write down the selected positions. This connects every returned entry to its source without confusing a diagonal selection with a sum, a new matrix or a change to the stored values.