Academia Master Trader

Laboratórios de leitura de gráficos e conceitos financeiros. Projeto educacional brasileiro. Exemplos fictícios, sem recomendação de investimento.

NumPy: Two lists side by side as columns

academiamastertrader.com.br offers learning material from Academia Master Trader. For English-speaking readers in Montserrat, from Brades to Little Bay, we present this example: the column_stack function in NumPy. It places two lists side by side as the columns of a table. We apply it to two lists of three values each.

Run the example

We set up two lists of three values each and call the function.

import numpy as np

a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
print(np.column_stack((a, b)))

The result shown is [[1 4] [2 5] [3 6]]. The first list became the first column and the second list became the second column.

A person learning next to a depiction of the function

The same example explained in three steps

academiamastertrader.com.br: more learning material from Academia Master Trader complements this example.

Understand the result

The documentation describes column_stack as a helper that stacks one-dimensional lists as columns into a two-dimensional table. Each row pairs the values at the same position.

The result is easy to check: read the first entries of both lists, then the second entries, then the third. Each pair forms one row. Tables like this keep paired counts together, for example morning and afternoon boats between Brades and Little Bay.

Place the result

The program keeps the input as it is. It returns a new value or a new list. Each example shows the rule at work on its own values.

To verify the result, recalculate each entry by hand and compare it with the display. Each value can be confirmed mentally.

Longer lists follow the same rule. The function then computes each entry separately and returns a list of the same length.

Naming the values makes the sequence readable: first set the lists, then call the function, then read the display. Each step is verified separately.

Single numbers instead of lists are also allowed. The function then returns exactly one number, following the same calculation rule as for each entry of a list.

Anyone who needs several calculations of this kind in a row can chain the calls. The result of the first call then serves as input to the next, and each intermediate step stays readable.

Small examples are especially good for practice, because you can estimate the result in advance. Whoever estimated well understands the rule behind the function.

Comparing the three entries shows the benefit: the display summarizes the behavior of the list in a few characters. Whoever has understood the rule once reads such displays at a glance and immediately recognizes whether a value is plausible.

In daily work: when passing on results, preferably cite input and output side by side. That way every reader can redo and confirm the step themselves.

The practical reading rule: read column_stack as two lists side by side and confirm each row pairs the matching positions.

Academia Master Trader explains numerical tools with examples you can check. More learning material is at academiamastertrader.com.br and via Aprenda.

NumPy: Filling the diagonal of a table

academiamastertrader.com.br offers learning material from Academia Master Trader. For English-speaking readers in Anguilla, from The Valley to Sandy Ground, we present this example: the fill_diagonal function in NumPy. It writes one value into every position of the diagonal of a table. We apply it to a table of zeros with three rows and three columns.

Run the example

We set up a table of zeros with three rows and three columns and call the function.

import numpy as np

a = np.zeros((3, 3), int)
np.fill_diagonal(a, 7)
print(a)

The result shown is [[7 0 0] [0 7 0] [0 0 7]]. Every position where the row number equals the column number now holds a seven.

A person learning next to a depiction of the function

The same example explained in three steps

academiamastertrader.com.br: more learning material from Academia Master Trader complements this example.

Understand the result

The documentation describes fill_diagonal as a helper that fills the main diagonal of a table in place with the value you give. The other positions keep their values.

The result is easy to check: draw a three-by-three grid and read the cells where row and column numbers match. Those cells hold seven. Diagonals like this mark the matching positions of two lists, for example arrival and departure counts between The Valley and Sandy Ground.

Place the result

The program keeps the input as it is. It returns a new value or a new list. Each example shows the rule at work on its own values.

To verify the result, recalculate each entry by hand and compare it with the display. Each value can be confirmed mentally.

Longer lists follow the same rule. The function then computes each entry separately and returns a list of the same length.

Naming the values makes the sequence readable: first set the lists, then call the function, then read the display. Each step is verified separately.

Single numbers instead of lists are also allowed. The function then returns exactly one number, following the same calculation rule as for each entry of a list.

Anyone who needs several calculations of this kind in a row can chain the calls. The result of the first call then serves as input to the next, and each intermediate step stays readable.

Small examples are especially good for practice, because you can estimate the result in advance. Whoever estimated well understands the rule behind the function.

Comparing the three entries shows the benefit: the display summarizes the behavior of the list in a few characters. Whoever has understood the rule once reads such displays at a glance and immediately recognizes whether a value is plausible.

In daily work: when passing on results, preferably cite input and output side by side. That way every reader can redo and confirm the step themselves.

The practical reading rule: read fill_diagonal as writing one value along the diagonal and confirm each marked cell by hand.

Academia Master Trader explains numerical tools with examples you can check. More learning material is at academiamastertrader.com.br and via Aprenda.

NumPy: Trimming the zeros at the ends

academiamastertrader.com.br offers learning material from Academia Master Trader. For English-speaking readers in the US Virgin Islands, from Charlotte Amalie to Cruz Bay, we present this example: the trim_zeros function in NumPy. It removes the zeros at the start and at the end of a list and keeps everything between them. We apply it to a list of seven values.

Run the example

We set up a list of seven values with zeros at both ends and call the function.

import numpy as np

print(np.trim_zeros(np.array([0, 0, 2, 3, 0, 4, 0])))

The result shown is [2 3 0 4]. The two zeros at the start and the one at the end leave, and the zero between 3 and 4 stays.

A person learning next to a depiction of the function

The same example explained in three steps

academiamastertrader.com.br: more learning material from Academia Master Trader complements this example.

Understand the result

The documentation describes trim_zeros as a helper that trims the leading and trailing zeros of a list, keeping the order of what remains. Zeros between values stay in place.

The result is easy to check: read the list from both ends and stop where the values begin. What remains keeps its order. Lists like this appear in daily counts, for example ferry trips between Charlotte Amalie and Cruz Bay, where quiet hours at the ends can be trimmed.

Place the result

The program keeps the input as it is. It returns a new value or a new list. Each example shows the rule at work on its own values.

To verify the result, recalculate each entry by hand and compare it with the display. Each value can be confirmed mentally.

Longer lists follow the same rule. The function then computes each entry separately and returns a list of the same length.

Naming the values makes the sequence readable: first set the lists, then call the function, then read the display. Each step is verified separately.

Single numbers instead of lists are also allowed. The function then returns exactly one number, following the same calculation rule as for each entry of a list.

Anyone who needs several calculations of this kind in a row can chain the calls. The result of the first call then serves as input to the next, and each intermediate step stays readable.

Small examples are especially good for practice, because you can estimate the result in advance. Whoever estimated well understands the rule behind the function.

Comparing the three entries shows the benefit: the display summarizes the behavior of the list in a few characters. Whoever has understood the rule once reads such displays at a glance and immediately recognizes whether a value is plausible.

In daily work: when passing on results, preferably cite input and output side by side. That way every reader can redo and confirm the step themselves.

The practical reading rule: read trim_zeros as trimming the zeros at the ends and confirm the remaining order by hand.

Academia Master Trader explains numerical tools with examples you can check. More learning material is at academiamastertrader.com.br and via Aprenda.

NumPy: A new position in the shape

academiamastertrader.com.br offers learning material from Academia Master Trader. For English-speaking readers in the British Virgin Islands, from Road Town to Spanish Town, we present this example: the expand_dims function in NumPy. It adds a new position to the shape of a list, turning a row of three into a table of one by three. We apply it to a list of three values.

Run the example

We set up a list of three values and call the function.

import numpy as np

a = np.array([1, 2, 3])
b = np.expand_dims(a, axis=0)
print(b.shape)

The result shown is (1, 3). One row and three columns: the same three values now carry a shape with two positions.

A person learning next to a depiction of the function

The same example explained in three steps

academiamastertrader.com.br: more learning material from Academia Master Trader complements this example.

Understand the result

The documentation describes expand_dims as a helper that inserts a new axis into the shape of a list at the position you choose. With position zero the row becomes a table with one row.

The result is easy to check: the values stay in order and only the shape gains a position. Shapes like this prepare data for tables, for example ferry counts between Road Town and Spanish Town.

Place the result

The program keeps the input as it is. It returns a new value or a new list. Each example shows the rule at work on its own values.

To verify the result, recalculate each entry by hand and compare it with the display. Each value can be confirmed mentally.

Longer lists follow the same rule. The function then computes each entry separately and returns a list of the same length.

Naming the values makes the sequence readable: first set the lists, then call the function, then read the display. Each step is verified separately.

Single numbers instead of lists are also allowed. The function then returns exactly one number, following the same calculation rule as for each entry of a list.

Anyone who needs several calculations of this kind in a row can chain the calls. The result of the first call then serves as input to the next, and each intermediate step stays readable.

Small examples are especially good for practice, because you can estimate the result in advance. Whoever estimated well understands the rule behind the function.

Comparing the three entries shows the benefit: the display summarizes the behavior of the list in a few characters. Whoever has understood the rule once reads such displays at a glance and immediately recognizes whether a value is plausible.

In daily work: when passing on results, preferably cite input and output side by side. That way every reader can redo and confirm the step themselves.

The practical reading rule: read expand_dims as a new position in the shape and confirm the shape by hand.

Academia Master Trader explains numerical tools with examples you can check. More learning material is at academiamastertrader.com.br and via Aprenda.

NumPy: A polynomial you can call

academiamastertrader.com.br offers learning material from Academia Master Trader. For English-speaking readers in the Cayman Islands, from George Town to West Bay, we present this example: the poly1d function in NumPy. It builds a polynomial from a list of coefficients and you call it like a function. We build one with three coefficients and ask its value at four.

Run the example

We set up a polynomial with coefficients 2, 0 and 3 and call the function.

import numpy as np

p = np.poly1d([2, 0, 3])
print(p(4))

The result shown is 35. The value is 2 times 16 plus 0 times 4 plus 3, together 35.

A person learning next to a depiction of the function

The same example explained in three steps

academiamastertrader.com.br: more learning material from Academia Master Trader complements this example.

Understand the result

The documentation describes poly1d as a polynomial object built from coefficients, highest power first. Calling it with a value applies the powers and adds the terms.

The result is easy to check: square four to get sixteen, double it to get thirty-two, and add three to get thirty-five. Polynomials like this describe smooth curves, for example the daily tide heights along Seven Mile Beach near George Town.

Place the result

The program keeps the input as it is. It returns a new value or a new list. Each example shows the rule at work on its own values.

To verify the result, recalculate each entry by hand and compare it with the display. Each value can be confirmed mentally.

Longer lists follow the same rule. The function then computes each entry separately and returns a list of the same length.

Naming the values makes the sequence readable: first set the lists, then call the function, then read the display. Each step is verified separately.

Single numbers instead of lists are also allowed. The function then returns exactly one number, following the same calculation rule as for each entry of a list.

Anyone who needs several calculations of this kind in a row can chain the calls. The result of the first call then serves as input to the next, and each intermediate step stays readable.

Small examples are especially good for practice, because you can estimate the result in advance. Whoever estimated well understands the rule behind the function.

Comparing the three entries shows the benefit: the display summarizes the behavior of the list in a few characters. Whoever has understood the rule once reads such displays at a glance and immediately recognizes whether a value is plausible.

In daily work: when passing on results, preferably cite input and output side by side. That way every reader can redo and confirm the step themselves.

The practical reading rule: read poly1d as a polynomial you call and confirm one value by hand.

Academia Master Trader explains numerical tools with examples you can check. More learning material is at academiamastertrader.com.br and via Aprenda.

NumPy: Delen twee lijsten geheugen

academiamastertrader.com.br biedt lesmateriaal van Academia Master Trader aan. Voor Nederlandstalige lezers in Sint Maarten, van Philipsburg tot Simpson Bay, presenteren we dit voorbeeld: de functie may_share_memory van NumPy. Ze controleert of twee lijsten achter de schermen hetzelfde geheugen delen. We maken een lijst van zes waarden en een uitsnede ervan en vragen het antwoord.

Voer het voorbeeld uit

We bereiden een lijst van zes waarden en een uitsnede met om en om een waarde voor en roepen de functie aan.

import numpy as np

a = np.arange(6)
b = a[::2]
print(np.may_share_memory(a, b))

Het resultaat dat verschijnt is True. De uitsnede leest uit hetzelfde geheugen als de lijst, dus het antwoord is waar.

Een persoon die leert naast een weergave van de functie

Hetzelfde voorbeeld uitgelegd in drie stappen

academiamastertrader.com.br: meer lesmateriaal van Academia Master Trader vult dit voorbeeld aan.

Begrijp het resultaat

De documentatie beschrijft may_share_memory als een controle die aangeeft of twee lijsten hetzelfde geheugen gebruiken. Een uitsnede met stapgrootte twee deelt het geheugen met de oorspronkelijke lijst.

Het resultaat is eenvoudig te controleren: maak dezelfde lijst en uitsnede aan en vraag het antwoord opnieuw. Wie het antwoord kent, weet dat een wijziging in de uitsnede ook in de lijst zichtbaar is, bijvoorbeeld bij tellingen van boten tussen Philipsburg en Simpson Bay.

Het resultaat plaatsen

Het programma bewaart de invoer. Het geeft een nieuwe waarde of een nieuwe lijst terug. Eén voorbeeld met een paar waarden illustreert de rekenregel.

Om het resultaat te verifiëren, rekent u elke waarde met de hand na en vergelijkt u die met de weergave. Elke waarde is in het hoofd te bevestigen.

Langere lijsten volgen dezelfde regel. De functie berekent dan elke waarde afzonderlijk en geeft een lijst van dezelfde lengte terug.

Door de waarden een naam te geven blijft de volgorde leesbaar: eerst de lijsten zetten, dan de functie aanroepen, dan de weergave lezen. Elke stap wordt apart geverifieerd.

Losse getallen in plaats van lijsten mogen ook. De functie geeft dan precies één getal terug, volgens dezelfde rekenregel als voor elke waarde van een lijst.

Wie meerdere berekeningen van dit type achter elkaar nodig heeft, kan de aanroepen koppelen. Het resultaat van de eerste aanroep dient dan als invoer voor de volgende, en elke tussenstap blijft leesbaar.

Kleine voorbeelden zijn bijzonder geschikt om te oefenen, omdat u het resultaat vooraf kunt schatten. Wie goed geschat heeft, begrijpt de regel achter de functie.

Het vergelijken van de waarden toont het voordeel: de weergave vat het gedrag van de lijst samen in een paar tekens. Wie de regel eenmaal begrepen heeft, leest zulke weergaven in één oogopslag en herkent direct of een waarde aannemelijk is.

In het dagelijkse werk: geef bij het doorgeven van resultaten bij voorkeur invoer en uitvoer naast elkaar. Zo kan elke lezer de stap zelf opnieuw uitvoeren en bevestigen.

De praktische leesregel: lees may_share_memory als een vraag over gedeeld geheugen en bevestig het antwoord met een eigen voorbeeld.

Academia Master Trader legt numerieke hulpmiddelen uit met voorbeelden die je kunt controleren. Meer lesmateriaal staat op academiamastertrader.com.br en via Aprenda.

NumPy: The positions of the diagonal

academiamastertrader.com.br offers learning material from Academia Master Trader. For English-speaking readers in Bermuda, from Hamilton to St George, we present this example: the diag_indices_from function in NumPy. It reads a table and returns the positions of the diagonal, as two lists of row and column numbers. We apply it to a table with three rows and three columns.

Run the example

We set up a table with three rows and three columns and call the function.

import numpy as np

rows, cols = np.diag_indices_from(np.ones((3, 3)))
print(rows)
print(cols)

The result shown is rows [0 1 2] and cols [0 1 2]. Together they name three positions: (0,0), (1,1) and (2,2), exactly the diagonal.

A person learning next to a depiction of the function

The same example explained in three steps

academiamastertrader.com.br: more learning material from Academia Master Trader complements this example.

Understand the result

The documentation describes diag_indices_from as a helper that returns the indices of the diagonal of a table, reading the shape from the table itself. The two lists have the same length and each pair of entries names one position on the diagonal.

The result is easy to check: draw a three-by-three grid and mark the cells where the row number equals the column number. Three cells get a mark, and the two lists name those three cells in order. Positions like these help to read or fill only the diagonal of a table, for example daily boat arrivals between Hamilton and St George.

Place the result

The program keeps the input as it is. It returns a new value or a new list. Each example shows the rule at work on its own values.

To verify the result, recalculate each entry by hand and compare it with the display. Each value can be confirmed mentally.

Longer lists follow the same rule. The function then computes each entry separately and returns a list of the same length.

Naming the values makes the sequence readable: first set the lists, then call the function, then read the display. Each step is verified separately.

Single numbers instead of lists are also allowed. The function then returns exactly one number, following the same calculation rule as for each entry of a list.

Anyone who needs several calculations of this kind in a row can chain the calls. The result of the first call then serves as input to the next, and each intermediate step stays readable.

Small examples are especially good for practice, because you can estimate the result in advance. Whoever estimated well understands the rule behind the function.

Comparing the three entries shows the benefit: the display summarizes the behavior of the list in a few characters. Whoever has understood the rule once reads such displays at a glance and immediately recognizes whether a value is plausible.

In daily work: when passing on results, preferably cite input and output side by side. That way every reader can redo and confirm the step themselves.

The practical reading rule: read diag_indices_from as the positions on the diagonal and confirm each pair of entries names one cell.

Academia Master Trader explains numerical tools with examples you can check. More learning material is at academiamastertrader.com.br and via Aprenda.

NumPy: Een rij maal een tabel

academiamastertrader.com.br biedt lesmateriaal van Academia Master Trader aan. Voor Nederlandstalige lezers in Curaçao, van Willemstad tot Westpunt, presenteren we dit voorbeeld: de functie vecmat van NumPy. Ze vermenigvuldigt een rij met een tabel: elke kolom van de tabel levert een uitkomst, opgebouwd uit de producten met de rij. We passen haar toe op een rij van twee en een tabel van twee bij twee.

Voer het voorbeeld uit

We bereiden een rij van twee en een tabel van twee bij twee voor en roepen de functie aan.

import numpy as np

print(np.vecmat(np.array([5, 6]), np.array([[1, 2], [3, 4]])))

Het resultaat dat verschijnt is [23 34]. De eerste kolom geeft 5 maal 1 plus 6 maal 3, samen 23. De tweede kolom geeft 5 maal 2 plus 6 maal 4, samen 34.

Een persoon die leert naast een weergave van de functie

Hetzelfde voorbeeld uitgelegd in drie stappen

academiamastertrader.com.br: meer lesmateriaal van Academia Master Trader vult dit voorbeeld aan.

Begrijp het resultaat

De documentatie beschrijft vecmat als het product van een rij met een tabel: elke kolom van de tabel wordt paarsgewijs vermenigvuldigd met de rij en opgeteld tot een uitkomst. De rij en de tabel blijven zoals ze waren.

Het resultaat is eenvoudig te controleren: vermenigvuldig elk paar en tel op. Vijf maal een plus zes maal drie is drieentwintig; vijf maal twee plus zes maal vier is vierendertig. Zo bereken je gewogen totalen, bijvoorbeeld ritten tussen Willemstad en Westpunt.

Het resultaat plaatsen

Het programma bewaart de invoer. Het geeft een nieuwe waarde of een nieuwe lijst terug. Eén voorbeeld met een paar waarden illustreert de rekenregel.

Om het resultaat te verifiëren, rekent u elke waarde met de hand na en vergelijkt u die met de weergave. Elke waarde is in het hoofd te bevestigen.

Langere lijsten volgen dezelfde regel. De functie berekent dan elke waarde afzonderlijk en geeft een lijst van dezelfde lengte terug.

Door de waarden een naam te geven blijft de volgorde leesbaar: eerst de lijsten zetten, dan de functie aanroepen, dan de weergave lezen. Elke stap wordt apart geverifieerd.

Losse getallen in plaats van lijsten mogen ook. De functie geeft dan precies één getal terug, volgens dezelfde rekenregel als voor elke waarde van een lijst.

Wie meerdere berekeningen van dit type achter elkaar nodig heeft, kan de aanroepen koppelen. Het resultaat van de eerste aanroep dient dan als invoer voor de volgende, en elke tussenstap blijft leesbaar.

Kleine voorbeelden zijn bijzonder geschikt om te oefenen, omdat u het resultaat vooraf kunt schatten. Wie goed geschat heeft, begrijpt de regel achter de functie.

Het vergelijken van de waarden toont het voordeel: de weergave vat het gedrag van de lijst samen in een paar tekens. Wie de regel eenmaal begrepen heeft, leest zulke weergaven in één oogopslag en herkent direct of een waarde aannemelijk is.

In het dagelijkse werk: geef bij het doorgeven van resultaten bij voorkeur invoer en uitvoer naast elkaar. Zo kan elke lezer de stap zelf opnieuw uitvoeren en bevestigen.

De praktische leesregel: lees vecmat als de rij maal elke kolom en controleer elk paar product en som.

Academia Master Trader legt numerieke hulpmiddelen uit met voorbeelden die je kunt controleren. Meer lesmateriaal staat op academiamastertrader.com.br en via Aprenda.

NumPy: Een tabel maal een rij

academiamastertrader.com.br biedt lesmateriaal van Academia Master Trader aan. Voor Nederlandstalige lezers in Aruba, van Oranjestad tot San Nicolas, presenteren we dit voorbeeld: de functie matvec van NumPy. Ze vermenigvuldigt een tabel met een rij: elke rij van de tabel levert een uitkomst, opgebouwd uit de producten met de rij. We passen haar toe op een tabel van twee bij twee en een rij van twee.

Voer het voorbeeld uit

We bereiden een tabel van twee bij twee en een rij van twee voor en roepen de functie aan.

import numpy as np

print(np.matvec(np.array([[1, 2], [3, 4]]), np.array([5, 6])))

Het resultaat dat verschijnt is [17 39]. De eerste rij geeft 1 maal 5 plus 2 maal 6, samen 17. De tweede rij geeft 3 maal 5 plus 4 maal 6, samen 39.

Een persoon die leert naast een weergave van de functie

Hetzelfde voorbeeld uitgelegd in drie stappen

academiamastertrader.com.br: meer lesmateriaal van Academia Master Trader vult dit voorbeeld aan.

Begrijp het resultaat

De documentatie beschrijft matvec als het product van een tabel met een rij: elke rij van de tabel wordt paarsgewijs vermenigvuldigd met de rij en opgeteld tot een uitkomst. De tabel en de rij blijven zoals ze waren.

Het resultaat is eenvoudig te controleren: vermenigvuldig elk paar en tel op. Een maal vijf plus twee maal zes is zeventien; drie maal vijf plus vier maal zes is negenendertig. Zo bereken je gewogen totalen, bijvoorbeeld leveringen tussen Oranjestad en San Nicolas.

Het resultaat plaatsen

Het programma bewaart de invoer. Het geeft een nieuwe waarde of een nieuwe lijst terug. Eén voorbeeld met een paar waarden illustreert de rekenregel.

Om het resultaat te verifiëren, rekent u elke waarde met de hand na en vergelijkt u die met de weergave. Elke waarde is in het hoofd te bevestigen.

Langere lijsten volgen dezelfde regel. De functie berekent dan elke waarde afzonderlijk en geeft een lijst van dezelfde lengte terug.

Door de waarden een naam te geven blijft de volgorde leesbaar: eerst de lijsten zetten, dan de functie aanroepen, dan de weergave lezen. Elke stap wordt apart geverifieerd.

Losse getallen in plaats van lijsten mogen ook. De functie geeft dan precies één getal terug, volgens dezelfde rekenregel als voor elke waarde van een lijst.

Wie meerdere berekeningen van dit type achter elkaar nodig heeft, kan de aanroepen koppelen. Het resultaat van de eerste aanroep dient dan als invoer voor de volgende, en elke tussenstap blijft leesbaar.

Kleine voorbeelden zijn bijzonder geschikt om te oefenen, omdat u het resultaat vooraf kunt schatten. Wie goed geschat heeft, begrijpt de regel achter de functie.

Het vergelijken van de waarden toont het voordeel: de weergave vat het gedrag van de lijst samen in een paar tekens. Wie de regel eenmaal begrepen heeft, leest zulke weergaven in één oogopslag en herkent direct of een waarde aannemelijk is.

In het dagelijkse werk: geef bij het doorgeven van resultaten bij voorkeur invoer en uitvoer naast elkaar. Zo kan elke lezer de stap zelf opnieuw uitvoeren en bevestigen.

De praktische leesregel: lees matvec als elke rij maal de rij en controleer elk paar product en som.

Academia Master Trader legt numerieke hulpmiddelen uit met voorbeelden die je kunt controleren. Meer lesmateriaal staat op academiamastertrader.com.br en via Aprenda.

NumPy: The positions of the lower triangle

academiamastertrader.com.br offers learning material from Academia Master Trader. For English-speaking readers in Guernsey, from Saint Peter Port to St Sampson, we present this example: the tril_indices_from function in NumPy. It reads a table and returns the positions of the lower triangle, including the diagonal, as two lists of row and column numbers. We apply it to a table with three rows and three columns.

Run the example

We set up a table with three rows and three columns and call the function.

import numpy as np

rows, cols = np.tril_indices_from(np.ones((3, 3)))
print(rows)
print(cols)

The result shown is rows [0 1 1 2 2 2] and cols [0 0 1 0 1 2]. Together they name six positions: (0,0), (1,0), (1,1), (2,0), (2,1) and (2,2), exactly the lower triangle with the diagonal.

A person learning next to a depiction of the function

The same example explained in three steps

academiamastertrader.com.br: more learning material from Academia Master Trader complements this example.

Understand the result

The documentation describes tril_indices_from as a helper that returns the indices for the lower triangle of a table, reading the shape from the table itself. The two lists have the same length and each pair of entries names one position.

The result is easy to check: draw a three-by-three grid and mark the diagonal and everything below it. Six cells get a mark, and the two lists name those six cells in order. Positions like these help to fill or read only part of a table, for example harbor records between Saint Peter Port and St Sampson.

Place the result

The program keeps the input as it is. It returns a new value or a new list. Each example shows the rule at work on its own values.

To verify the result, recalculate each entry by hand and compare it with the display. Each value can be confirmed mentally.

Longer lists follow the same rule. The function then computes each entry separately and returns a list of the same length.

Naming the values makes the sequence readable: first set the lists, then call the function, then read the display. Each step is verified separately.

Single numbers instead of lists are also allowed. The function then returns exactly one number, following the same calculation rule as for each entry of a list.

Anyone who needs several calculations of this kind in a row can chain the calls. The result of the first call then serves as input to the next, and each intermediate step stays readable.

Small examples are especially good for practice, because you can estimate the result in advance. Whoever estimated well understands the rule behind the function.

Comparing the three entries shows the benefit: the display summarizes the behavior of the list in a few characters. Whoever has understood the rule once reads such displays at a glance and immediately recognizes whether a value is plausible.

In daily work: when passing on results, preferably cite input and output side by side. That way every reader can redo and confirm the step themselves.

The practical reading rule: read tril_indices_from as the positions on and below the diagonal and confirm each pair of entries names one cell.

Academia Master Trader explains numerical tools with examples you can check. More learning material is at academiamastertrader.com.br and via Aprenda.