NumPy triu keeps an upper triangle while replacing entries below its boundary with zeros
A triangular arrangement in an array concerns coordinates rather than the size of the numbers. Keeping the upper triangle does not mean keeping the largest values. Academia Master Trader explains NumPy triu in English for readers in Croatia. We will use a small matrix to follow the main boundary and then move that boundary with the k argument. Every result can be checked against the input positions. These examples describe data organisation, not financial outcomes, forecasts or additional observations beyond the supplied integers.
Write the complete input first
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 sets of indices run from zero through two. The main diagonal contains eight at (0,0), nine at (1,1) and one at (2,2). Entries above that diagonal have a column index greater than their row index.
The call np.triu(a) returns [[8,3,6],[0,9,4],[0,0,1]], also with shape (3,3). It keeps entries on and above the main diagonal and replaces entries below it with zeros. The input positions containing two, seven and five correspond to zeros in the output. Other values stay at their original coordinates. No row or column is removed from the result. The output still contains nine positions, including the three positions now occupied by replacement zeros.
Distinguish a zeroed position from an omitted position
The second output row is [0,9,4], not [9,4]. Its initial zero preserves the first column position. The final row is [0,0,1], preserving all three column positions even though only one original value remains there. These zeros are part of the returned arrangement. They are not evidence that the original data contained zero observations at those positions.
The documentation describes triu as returning a copy with entries below the selected diagonal zeroed. For this operation, reading the output should therefore not be confused with deleting parts of the original matrix. In our example, the shape remains three by three and the retained values do not move. The operation changes specified output entries rather than producing a compact list of selected values.

Read the figure by coordinates
The figure places the input beside the result for the default boundary. Gold cells identify the retained upper region in the result; grey cells contain the replacement zeros below it. Follow four at (1,2). Its column index is greater than its row index, so it remains four at the same output coordinate. Two, at (1,0), is below the boundary and becomes zero in the output.
Nine is kept because of its position on the main diagonal, not because it is the largest supplied number. One is kept for exactly the same positional reason. Seven is larger than one but lies below the boundary, so its output position becomes zero. This contrast separates a geometric rule from a numerical ranking or a threshold applied to the values.
Move the boundary above the main diagonal
The call np.triu(a,k=1) gives [[0,3,6],[0,0,4],[0,0,0]]. Only entries above the main diagonal are retained. The main diagonal itself is now below the selected first upper diagonal and is zeroed. The shape remains (3,3). A positive k moves the retention boundary upwards in the familiar printed matrix layout; it does not add one to the retained values.
For k=2, only six remains from this input, giving [[0,0,6],[0,0,0],[0,0,0]]. The value six occupies (0,2), on the second upper diagonal. There is no additional entry further along that diagonal within the matrix. This example makes the selected boundary visible without changing the input dimensions.
Move the boundary below the main diagonal
The call np.triu(a,k=-1) gives [[8,3,6],[2,9,4],[0,5,1]]. It retains the first diagonal below the main diagonal as well as everything above it. Only seven at (2,0) remains below the selected boundary and is zeroed. A negative k changes the boundary position; it does not request negative numerical entries or subtract from the retained numbers.
Continue learning with Academia Master Trader at academiamastertrader.com.br and in Aprenda. Record the input shape and k value. Mark the retained coordinates, write replacement zeros in the remaining positions and confirm that the full shape is preserved. Those checks explain the upper triangle without confusing it with sorting or removing rows.