mindspore.numpy.take

mindspore.numpy.take(a, indices, axis=None, mode='clip')[源代码]

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,...].

说明

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

参数
  • 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. Defaults to None.

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

    Specifies how out-of-bounds indices will behave. Defaults to ‘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.

返回

Tensor, the indexed result.

异常
Supported Platforms:

Ascend GPU CPU

样例

>>> 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]]