NumPy triu retains an upper triangular region without shrinking the matrix
An upper triangular selection keeps positions on one side of a diagonal boundary. NumPy triu preserves those entries and replaces positions below the boundary with zero. Academia Master Trader presents this English lesson for readers in Eritrea, using constructed arrays to explain offset direction, matrix shape and the difference between positional selection and numeric filtering.
The main diagonal belongs to the default retained region
After import numpy as np, define m = np.array([[3,5,7],[8,10,12],[13,15,17]]). The call np.triu(m) returns [[3,5,7],[0,10,12],[0,0,17]].
The main diagonal entries three, ten and seventeen remain, together with the entries above them. The lower-left positions become zero. No entries move upwards: every retained value stays at its original row and column coordinate.
Using zero-based indices, the default retained region contains positions whose column index is at least their row index. The first row therefore retains three entries, the second row retains two and the third row retains one in this square example.

A positive offset excludes the main diagonal
The call np.triu(m,k=1) returns [[0,5,7],[0,0,12],[0,0,0]]. Its boundary is the first diagonal above the main diagonal. Only that boundary and the positions above it remain.
Positive k does not mean that more positions are always retained. For triu, moving the boundary upwards makes the retained upper region smaller. The original main diagonal becomes zero when k is one.
The output still has shape (3,3). Excluded positions are represented by zeros rather than removed from the array. That is why reading the result as a compact collection of three selected values would describe a different output structure.
A negative offset includes an additional lower diagonal
The call np.triu(m,k=-1) returns [[3,5,7],[8,10,12],[0,15,17]]. The boundary moves one diagonal below the main diagonal, so eight and fifteen are now retained. Only the lower-left entry thirteen is replaced with zero.
The general positional rule retains entries where column index minus row index is greater than or equal to k. This rule applies without comparing the actual numeric values. Negative entries in a retained position would remain negative, and positive entries outside the region would still become zero.
An output zero alone does not reveal whether the original entry was excluded. A retained entry might already have been zero. Keep the positional rule available when that distinction matters.
The result is a copy with the same shape
Triu returns a copy and preserves the input data type. Editing the result does not change the source matrix. The operation does not sort values, sum the region or produce only its coordinate list.
Rectangular matrices also work. For arrays with more than two dimensions, the triangular rule applies to the final two axes across the leading dimensions. Identify those matrix axes before explaining a layered array.
Continue learning with Academia Master Trader at academiamastertrader.com.br and in Aprenda. Record the offset, retained-position rule and output shape beside each upper triangular result.