NumPy triu keeps the upper part of an array and replaces entries below a diagonal with zeros
Keeping the upper part of a matrix does not require removing its lower rows or changing its rectangular shape. NumPy triu returns a copy in which entries below a selected diagonal are replaced with zeros. The remaining positions keep their original values. The name describes a position rule, not a search for large numbers.
Academia Master Trader presents this explanation in English for readers in Zimbabwe. The examples concern array organisation only. They do not represent prices, trading signals or financial results. The aim is to understand the boundary defined by a diagonal and to distinguish zeroing selected positions from extracting a smaller collection of entries.
Start with the main diagonal
Let x have rows [1,2,3], [4,5,6] and [7,8,9], giving it shape (3,3). The expression np.triu(x) produces rows [1,2,3], [0,5,6] and [0,0,9]. The output still has three rows and three columns.
The default diagonal is the main diagonal. Values one, five and nine remain on that boundary. Values two, three and six remain above it. The positions originally holding four, seven and eight are below the boundary, so the output holds zeros in those positions instead.

The zeros are actual values in the output array, not blank spaces. They do not indicate that NumPy failed to obtain an observation. They result from the chosen structural operation. If the entries carry a real-world meaning, that meaning must be considered separately from the matrix layout.
Describe the boundary with indices
Using zero-based indices, the main diagonal contains positions where the row and column indices are equal. Above that diagonal, the column index is greater than the row index. Below it, the row index is greater than the column index.
For the default operation, triu retains positions where the column index is at least the row index. It zeros the positions that do not satisfy that relationship. This describes why the value four at coordinate (1,0) is replaced, while six at (1,2) remains.
The comparison concerns indices, not the size or sign of each entry. A negative value above the selected diagonal remains negative. A large positive value below it becomes zero. Reordering the numeric values without changing their positions does not change which locations belong to the upper region.
Shift the diagonal with k
The parameter k chooses the boundary diagonal. Its default is zero. A positive value moves the boundary above the main diagonal. For the same x, np.triu(x,k=1) returns rows [0,2,3], [0,0,6] and [0,0,0].
That result keeps the entries strictly above the main diagonal and zeros the main diagonal itself along with the region below it. The original values are not shifted into neighbouring cells. The boundary changes, while each surviving value stays at its existing coordinate.
A negative k moves the boundary below the main diagonal. The expression np.triu(x,k=-1) gives rows [1,2,3], [4,5,6] and [0,8,9]. In this case, only seven is below the selected boundary. Including one lower diagonal retains more entries than the default operation.
A rectangular array still has a diagonal rule
The input need not be square. Consider rows [1,2,3], [4,5,6], [7,8,9] and [10,11,12]. This array has shape (4,3). With k equal to minus one, the result has rows [1,2,3], [4,5,6], [0,8,9] and [0,0,12].
This example follows the same coordinate rule even though the array has more rows than columns. The last value twelve sits on the first diagonal below the main diagonal, so it remains. Ten and eleven lie farther below the selected boundary and become zero.
A rectangular outline does not prevent the operation from defining an upper region. It simply changes how many positions each row has on either side of the boundary. Describing the coordinates is more precise than expecting every output to look like a geometrically complete triangle.
Do not confuse zeroing with extraction
The result of triu remains an arranged array with the original matrix shape. It is not a compact vector containing only retained entries. Nor does it add those entries together. A later operation may extract positions or calculate a sum, but those are separate steps.
The official reference describes triu as returning a copy. This is different from a description that promises to edit the original input directly. Keep the output in a named variable when the program needs to use the upper-region arrangement in subsequent work.
For arrays with more than two dimensions, NumPy applies triu to the final two axes. The earlier axes identify the surrounding collections. Before interpreting a multidimensional result, identify which two dimensions form those matrix-like planes rather than assuming that the first two axes define the operation.
Connect the rule to the intended structure
Continue learning with Academia Master Trader at academiamastertrader.com.br. Aprenda provides access to educational material. Name the shape, identify the boundary diagonal and follow one retained position and one zeroed position. This keeps the geometric rule distinct from numerical ranking, data selection and later calculations.