NumPy take selects array entries in the order specified by integer positions

A list of positions can describe a deliberate selection from an array. NumPy take retrieves entries in that requested order, including repeated positions when they appear in the index list. Academia Master Trader explains the operation in English for readers in Ukraine. Small constructed vectors and matrices show how axis selection changes the meaning of a position and how an index array can shape the output.

Select positions rather than numerical values

After import numpy as np, define x = np.array([10,20,30,40]). The call np.take(x,[3,0,3]) returns [40,10,40]. Position three supplies forty, position zero supplies ten and the repeated position three supplies forty again. The function follows the index sequence rather than sorting the selected values.

Python uses zero based indexing here: the first entry is at position zero. The list [3,0,3] names positions, not the values three and zero. Repetition in the result comes from repetition in the selection. It does not mean that the original vector has gained another independently recorded entry.

NumPy take

Select columns with an explicit axis

Define m = np.array([[1,2,3],[4,5,6]]), which has shape (2,3). The expression np.take(m,[2,0],axis=1) returns [[3,1],[6,4]]. It selects column position two and then column position zero while keeping both rows. The result has shape (2,2).

With np.take(m,[1,0],axis=0), the result is [[4,5,6],[1,2,3]]. This selection requests row position one before row position zero. The numerical index one has a different role depending on the selected axis. Write both the axis and the intended positions when explaining a matrix selection.

Omitting the axis uses a flattened input

The call np.take(m,[2,0]) returns [3,1]. With the default axis value of None, take treats the input as flattened instead of selecting complete columns. In this example, the flattened sequence is [1,2,3,4,5,6]. Positions two and zero therefore retrieve three and one.

Compare that one dimensional result with the two dimensional column selection made using axis one. The same index list can produce different shapes and groupings because the axis setting defines the selection context. Include that setting in a reusable example rather than relying on a reader to infer it from the returned numbers.

The index array can have its own shape

For the original vector x, np.take(x,[[0,2],[3,1]]) returns [[10,30],[40,20]]. The two by two arrangement of indices produces a two by two arrangement of selected entries. Each index supplies one value from the vector at the corresponding output position.

For a general axis selection, the index array's dimensions replace the selected axis in the output shape. Dimensions before and after that axis remain around them. For example, applying that two by two index array to the columns of a two row matrix would add both index dimensions between the retained row dimension and any dimensions that follow the selected axis.

Decide how outside positions should behave

The default mode raises an error for an index outside the valid range. The mode named wrap maps indices around the axis length. The mode named clip restricts them to the range endpoints. For x, np.take(x,[4],mode='wrap') returns [10], while np.take(x,[4],mode='clip') returns [40].

Continue learning with Academia Master Trader at academiamastertrader.com.br and in Aprenda. Record the input shape, selected axis, index sequence and boundary mode. Check output shape as well as values. These details explain the selection without confusing repeated retrieval with additional source data.