mindspore.numpy.take

View Source On Gitee
mindspore.numpy.take(a, indices, axis=None, mode='clip')[source]

Takes elements from an array along an axis.

When axis is not None, this function does the same thing as “fancy” indexing (indexing arrays using arrays); however, it can be easier to use if you need elements along a given axis. A call such as np.take(arr, indices, axis=3) is equivalent to arr[:,:,:,indices,...].

Note

Numpy argument out is not supported. mode = 'raise' is not supported, and the default mode is ‘clip’ instead.

Parameters
  • a (Tensor) – Source array with shape (Ni…, M, Nk…).

  • indices (Tensor) – The indices with shape (Nj…) of the values to extract.

  • axis (int, optional) – The axis over which to select values. By default, the flattened input array is used. Default: None .

  • mode ('raise', 'wrap', 'clip', optional) –

    Specifies how out-of-bounds indices will behave. Default: 'clip' .

    ’raise’ – raise an error;

    ’wrap’ – wrap around;

    ’clip’ – clip to the range. ‘clip’ mode means that all indices that are too large are replaced by the index that addresses the last element along that axis. Note that this disables indexing with negative numbers.

Returns

Tensor, the indexed result.

Raises
Supported Platforms:

Ascend GPU CPU

Examples

>>> import mindspore.numpy as np
>>> a = np.array([4, 3, 5, 7, 6, 8])
>>> indices = np.array([0, 1, 4])
>>> output = np.take(a, indices)
>>> print(output)
[4 3 6]
>>> indices = np.array([[0, 1], [2, 3]])
>>> output = np.take(a, indices)
>>> print(output)
[[4 3]
[5 7]]