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

Selecting positions does not require those positions to be consecutive or sorted. NumPy take gathers entries using integer indices and an optional axis. Academia Master Trader presents this English lesson for readers in Israel, using constructed arrays to explain selection order, repeated indices and how the shape of the index array affects the result.

The index list determines the output order

After import numpy as np, define v = np.array([4,9,14,19]). The call np.take(v,[3,0,2]) returns [19,4,14]. NumPy counts positions from zero, so index three selects nineteen and index zero selects four.

The output follows the supplied indices, not the original numerical order. The call does not sort the source values. An index describes a position rather than a target value: asking for index two selects fourteen, not the number two. Keep these two meanings separate when reading a selection expression.

NumPy indexed selection

Repeated indices produce repeated selections

The call np.take(v,[1,1,0]) returns [9,9,4]. The same input position can contribute more than once. This is useful for understanding that take follows an explicit positional recipe rather than collecting a set of unique values.

A negative index is valid under the default mode when it falls within the input bounds. For example, np.take(v,[-1,0]) returns [19,4]. Negative one refers to the final position. An index beyond the allowed range raises IndexError under the default mode.

An axis selects corresponding slices of a matrix

Define m = np.array([[2,5,8],[11,14,17]]). The call np.take(m,[2,0],axis=1) returns [[8,2],[17,11]], with shape (2,2). It selects column two and then column zero across both rows.

Using np.take(m,[1,0],axis=0) instead returns [[11,14,17],[2,5,8]]. Whole rows are selected in the requested order. Both the index sequence and the chosen axis are necessary to describe which entries appear and how they are arranged.

Without an axis, positions refer to the flattened input

The call np.take(m,[3,0]) returns [11,2]. With axis omitted, NumPy uses the flattened input sequence. Position three is eleven in that sequence, even though the matrix has only two rows.

The index array can also have its own dimensions. The call np.take(v,[[0,3],[2,1]]) returns [[4,19],[14,9]], with shape (2,2). For a selected axis, the index array's shape replaces that axis in the output shape, while the surrounding source axes remain.

A scalar index and a one entry index list also describe different shapes. Selecting np.take(m,1,axis=0) returns a row shaped vector with shape (3,). Selecting np.take(m,[1],axis=0) keeps the index list dimension and returns shape (1,3). The values match, but the dimensional structure differs.

Choose boundary behaviour deliberately

Take supports raise, wrap and clip modes. With this four entry vector, np.take(v,[4],mode='wrap') returns [4], while np.take(v,[4],mode='clip') returns [19]. Wrap returns around the boundary; clip limits an excessive index to an endpoint.

Clip also changes negative index behaviour: np.take(v,[-1],mode='clip') returns [4], not [19]. State the mode explicitly when boundary handling is part of the intended calculation.

Continue learning with Academia Master Trader at academiamastertrader.com.br and in Aprenda. Record the index order, axis and mode beside every selection result.