NumPy tril preserves the lower region of a matrix and zeros positions above its diagonal boundary
An array's lower triangle is defined by row and column positions. It is not a collection of the smallest numbers, and keeping it does not mean deleting the upper rows. Academia Master Trader explains NumPy tril in English for readers in Romania. We will read a three by three matrix, mark the default boundary and compare two shifted boundaries. The supplied entries make every output directly checkable. These examples concern data organisation, not financial outcomes or additional observations. The distinction between a retained value and a replacement zero will guide the explanation.
Identify the main diagonal
After import numpy as np, define a = np.array([[8,3,6],[2,9,4],[7,5,1]]). Its shape is (3,3). Row and column indices each run from zero through two. The main diagonal connects positions (0,0), (1,1) and (2,2), containing eight, nine and one. A position below this diagonal has a row index greater than its column index. A position above it has the opposite relationship.
The call np.tril(a) gives [[8,0,0],[2,9,0],[7,5,1]], with the same shape (3,3). It retains entries on and below the main diagonal and replaces entries above it with zeros. Three, six and four are replaced in their respective output positions. The retained values stay at their original coordinates, so two remains at (1,0) and five remains at (2,1).
Keep every row and column position
The first output row is [8,0,0], not simply [8]. Its two zeros preserve the second and third column positions. The second row is [2,9,0], while the final row remains [7,5,1]. All nine positions still exist in the output. Reading the result as three rows of different lengths would describe a different structure from the actual three by three array.
NumPy documents tril as returning a copy whose entries above the selected diagonal are zeroed. In this example, the original matrix supplies all retained numbers, while the zeros describe the operation's replacement rule. They do not mean the original observations at those coordinates were zero. The function also does not move selected entries together to form a compact one dimensional sequence.

Read the lower region in the figure
The figure shows the complete input beside the default output. Gold cells in the output contain retained entries; grey cells contain replacement zeros. Follow seven at (2,0). It is below the main diagonal and stays seven at that coordinate. Follow six at (0,2). It is above the main diagonal, so its corresponding output position contains zero.
The largest supplied number, nine, remains because it sits on the main diagonal. One remains for the same positional reason. Seven is retained even though it is larger than several entries that are replaced. This makes the rule different from keeping values below a numerical threshold. Coordinates decide which positions are zeroed, independently of the relative sizes of their contents.
Use a negative k for a stricter lower region
The call np.tril(a,k=-1) gives [[0,0,0],[2,0,0],[7,5,0]]. The selected boundary is the first diagonal below the main diagonal. Entries on the main diagonal are now above that boundary, so eight, nine and one are zeroed as well. The retained entries are two, seven and five. The shape is still (3,3) because the boundary changes the retained region, not the dimensions.
A negative k is not a request for negative numerical values. Our input contains only positive integers, yet this call has a clear result. It also does not subtract one from any retained entry. Two stays two, seven stays seven and five stays five. Write the chosen diagonal beside the matrix before interpreting the sign as an instruction about arithmetic.
Use a positive k to retain more positions
The call np.tril(a,k=1) gives [[8,3,0],[2,9,4],[7,5,1]]. It includes the first diagonal above the main diagonal as well as everything below it. Only six, at (0,2), is above the selected boundary and is replaced. With k=2, all entries of this particular matrix are retained. That statement depends on its dimensions, rather than being a general promise about every input.
Continue learning with Academia Master Trader at academiamastertrader.com.br and in Aprenda. Record the shape and k value, mark the diagonal boundary and write a zero in every output position above it. Then trace two retained coordinates to confirm that their values and positions remain unchanged.