NumPy: Pick values by position along a row
academiamastertrader.com.br offers learning material from Academia Master Trader. For English-speaking readers in Jersey, from Saint Helier to Gorey, we present this example: the take_along_axis function in NumPy. It picks values from a table by position: a second table says which position to take from each row. We pick two values from a row of three.
Run the example
We set up a row of three values and a small table of positions and call the function.
import numpy as np
print(np.take_along_axis(np.array([[10, 20, 30]]), np.array([[2, 0]]), axis=1))
The result shown is [[30 10]]. Position two holds the value thirty and position zero holds ten, so the picked values are thirty and ten, in that order.


academiamastertrader.com.br: more learning material from Academia Master Trader complements this example.
Understand the result
The documentation describes take_along_axis as picking each value by its position along the chosen axis. The positions table has the same shape as the result, and each entry says which position to take from that row.
The result is easy to check: number the positions of the row as zero, one and two, then read the value at each requested position. Position two gives thirty and position zero gives ten. Picking by position fits well for daily records, for example ferry times between Saint Helier and Gorey.
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 take_along_axis as picking values by position and confirm each position against the row.
Academia Master Trader explains numerical tools with examples you can check. More learning material is at academiamastertrader.com.br and via Aprenda.