NumPy triu keeps an upper triangular region while replacing entries below its boundary with zero
An upper triangular array is defined by positions, not by selecting the largest numeric values. NumPy triu returns an array with entries below a chosen diagonal set to zero. Academia Master Trader presents this English lesson for readers in Saudi Arabia, using constructed matrices to explain the default boundary, positive and negative offsets and the way the rule applies to the final two axes of a higher dimensional array.
The default boundary includes the main diagonal
After import numpy as np, define m = np.array([[2,4,6],[8,10,12],[14,16,18]]). The call u = np.triu(m) returns [[2,4,6],[0,10,12],[0,0,18]]. The shape remains (3,3).
Positions on or above the main diagonal retain their original entries. Positions below it receive zero. With zero based row and column indices, an entry is retained when its column index is at least its row index. This is a positional condition. It does not depend on whether a value is large, small, positive or negative.

A positive offset moves the boundary upward
The call np.triu(m,k=1) returns [[0,4,6],[0,0,12],[0,0,0]]. The main diagonal entries are now zero because the retained region starts at the diagonal one position above the main one.
The output still includes every position above that selected diagonal, not only the entries on the selected diagonal itself. In this example, the entry six at position (0,2) remains too. Distinguish an upper triangular region from a diagonal extraction, which produces a sequence rather than preserving a full two dimensional arrangement.
A negative offset includes more entries below the main diagonal
The call np.triu(m,k=-1) returns [[2,4,6],[8,10,12],[0,16,18]]. It retains the first diagonal below the main one and everything above that boundary. Only the entry fourteen is replaced with zero here.
The sign of k changes which side of the main diagonal the boundary lies on. A negative offset does not ask the function to keep only the lower triangle. State both the selected boundary and the direction of the retained region when explaining an offset. For triu, the retained region remains on or above the chosen diagonal.
The operation returns a copy rather than changing m
NumPy documents the result as a copy. In the default example, writing u[0,1] = 99 changes the returned array without changing m[0,1], which remains four. The original m also keeps its entries below the main diagonal.
A zero in the output does not by itself reveal whether the corresponding input was zero. It may be an original zero in the retained region or a replacement below the boundary. Keep the positional rule available when interpreting the result rather than treating all zeros as observations from the original matrix.
Higher dimensional input uses its final two axes
Define b = np.array([[[1,2],[3,4]],[[5,6],[7,8]]]), with shape (2,2,2). The call np.triu(b) returns [[[1,2],[0,4]],[[5,6],[0,8]]], with the same shape. Each final two dimensional slice receives the upper triangle rule.
The leading axis is not the row axis for this operation. Continue learning with Academia Master Trader at academiamastertrader.com.br and in Aprenda. Record the selected offset, retained region and output shape so that triangular selection is not confused with sorting, summing or extracting a single diagonal.