NumPy take selects positions in the requested order along a chosen axis

Selecting entries from an array requires a clear distinction between values and positions. Asking for index two does not mean asking for the numerical value two. Academia Master Trader explains NumPy take in English for readers in Czechia. We will use a small matrix to follow column selection, repeated indices and the default flattened interpretation. The supplied integers are enough to see each result directly. These examples describe data organisation rather than financial outcomes, rankings or additional observations beyond the original inputs.

Write the input with its coordinates

After import numpy as np, define a = np.array([[8,3,6],[2,9,4]]). Its shape is (2,3): two rows and three columns. Row indices are zero and one. Column indices are zero, one and two. The value six occupies (0,2), while four occupies (1,2). Both are in column two, even though neither has the numerical value two.

The call np.take(a,[2,0],axis=1) selects column two and then column zero. It gives [[6,8],[4,2]] with shape (2,2). The first output row comes from the first input row, and the second output row comes from the second input row. Within each row, the requested column order determines the result. Selecting columns does not reorder the rows.

Read the index sequence from left to right

The index list [2,0] is a sequence of instructions about positions. Its first entry chooses the third original column, and its second chooses the first original column. This is why six appears before eight in the first output row. The function does not sort the selected values and does not restore the columns to their original order after selecting them.

Changing the sequence to [0,2] changes the result to [[8,6],[2,4]]. The same two source columns are involved, but the output order is different. The shape remains (2,2). Shape alone therefore cannot describe which values were selected or how they are ordered. Keep the actual index sequence beside the resulting shape when recording a selection. A useful written record has a separate line for the source, the axis and the requested positions. This keeps the meaning visible even when the selected values happen to repeat or resemble their own index numbers.

NumPy take

Repeated indices repeat selected entries

The figure shows the input matrix and the output for columns two and zero. It labels the requested sequence separately from the input shape. Trace four from the second row's final column to the second output row's first column. The value remains four, while its local column coordinate changes from two to zero.

The call np.take(a,[1,1,0],axis=1) gives [[3,3,8],[9,9,2]], with shape (2,3). Index one appears twice in the sequence, so the corresponding column appears twice in the output. This is deliberate selection with repetition, not a newly measured entry. The selected axis length follows the length of this one dimensional index sequence, rather than the number of distinct indices.

Choose rows by naming the row axis

The call np.take(a,[1,0],axis=0) gives [[2,9,4],[8,3,6]], with shape (2,3). Now the sequence selects rows instead of columns. The second input row comes first, followed by the first input row. Each selected row keeps all three of its columns in their original order. The index list has the same numbers as a possible column list, but the axis changes what those numbers address.

For this matrix, row index two is outside the selected row axis, whose length is only two. Column index two is valid because the column axis has length three. Always check indices against the length of the named axis. A valid position on one axis is not automatically valid on another.

Distinguish an omitted axis from column selection

When axis is omitted, NumPy take uses the flattened input. Here np.take(a,[2,0]) gives [6,8], with shape (2,). It selects positions from the flattened sequence [8,3,6,2,9,4], rather than selecting whole columns from both rows. This differs from the explicit column result [[6,8],[4,2]]. The omitted argument changes the interpretation, not merely the printed layout.

Continue learning with Academia Master Trader at academiamastertrader.com.br and in Aprenda. Record the input shape, selected axis and ordered index sequence. Then trace one source coordinate into its output coordinate. Those checks separate a position request from a value request and make repeated selections easy to explain.