NumPy trace adds diagonal entries and removes the two selected matrix axes
A diagonal total answers a different question from a row total or a complete matrix sum. NumPy trace adds entries along one diagonal within two selected axes. Academia Master Trader presents this English lesson for readers in Ethiopia, using constructed arrays to make diagonal offsets, axis defaults and output shape easier to follow.
Start with a diagonal rather than the entire matrix
After import numpy as np, define m = np.array([[4,6,8],[9,11,13],[14,16,18]]). The call np.trace(m) returns 33, because the main diagonal contains four, eleven and eighteen.
The result does not include every value in the matrix. It also does not produce three separate row totals. It reduces the selected diagonal to one sum. The original array remains available for another calculation.
The main diagonal joins positions whose row and column indices match. In this example, these positions are (0,0), (1,1) and (2,2). Thinking in coordinates helps separate the diagonal rule from the actual numbers stored there.

Offset changes which diagonal contributes
The call np.trace(m,offset=1) returns 19, from six plus thirteen. A positive offset chooses a diagonal above the main diagonal, towards larger column indices.
The call np.trace(m,offset=-1) returns 25, from nine plus sixteen. A negative offset chooses a diagonal below the main diagonal, towards larger row indices.
Changing offset does not shift values inside the array. It changes the positions selected for addition. These shorter diagonals contain two entries, while the main diagonal in this square example contains three. Rectangular matrices can have diagonals of different lengths too.
Do not confuse this operation with returning the diagonal entries themselves. A diagonal extraction would preserve the contributing entries as a sequence; trace combines them into their total.
Higher-dimensional arrays need explicit axis attention
For trace, the default matrix axes are axis zero and axis one. They are not automatically the final two axes. This detail matters when an array stores several matrices in layers.
Define layers = np.arange(8).reshape(2,2,2). Then np.trace(layers) returns [6,8]. In contrast, np.trace(layers,axis1=1,axis2=2) returns [3,11].
Both outputs have shape (2,), but they describe different diagonal selections. A matching output shape therefore does not prove that two calculations selected the same entries.
The output shape removes axis1 and axis2 from the input shape. Write down the axes and their meaning before describing which remaining dimension labels the totals.
Choose an accumulator type deliberately when needed
Trace accepts dtype to choose the accumulator and returned data type. By default, an integer input narrower than the default integer precision is promoted for the sum; otherwise the input precision is used. An explicit dtype can make the intended accumulation type clear.
Continue learning with Academia Master Trader at academiamastertrader.com.br and in Aprenda. Keep the diagonal offset, selected axes and expected output shape beside every trace calculation.