dpnp.take

dpnp.take(a, indices, /, *, axis=None, out=None, mode='wrap')[source]

Take 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 dpnp.take(a, indices, axis=3) is equivalent to a[:, :, :, indices, ...].

For full documentation refer to numpy.take.

Parameters:
  • a ({dpnp.ndarray, usm_ndarray}, (Ni..., M, Nk...)) -- The source array.

  • indices ({array_like, scalars}, (Nj...)) -- The indices of the values to extract. Also allow scalars for indices.

  • axis ({None, int, bool, 0-d array of integer dtype}, optional) -- The axis over which to select values. By default, the flattened input array is used. Default: None.

  • out ({None, dpnp.ndarray, usm_ndarray}, optional (Ni..., Nj..., Nk...)) -- If provided, the result will be placed in this array. It should be of the appropriate shape and dtype. Default: None.

  • mode ({"wrap", "clip"}, optional) --

    Specifies how out-of-bounds indices will be handled. Possible values are:

    • "wrap": clamps indices to (-n <= i < n), then wraps negative indices.

    • "clip": clips indices to (0 <= i < n).

    Default: "wrap".

Returns:

out -- The returned array has the same type as a.

Return type:

dpnp.ndarray, (Ni..., Nj..., Nk...)

See also

dpnp.compress

Take elements using a boolean mask.

dpnp.ndarray.take

Equivalent method.

dpnp.take_along_axis

Take elements by matching the array and the index arrays.

Examples

>>> import dpnp as np
>>> x = np.array([4, 3, 5, 7, 6, 8])
>>> indices = np.array([0, 1, 4])
>>> np.take(x, indices)
array([4, 3, 6])

In this example "fancy" indexing can be used.

>>> x[indices]
array([4, 3, 6])
>>> indices = dpnp.array([-1, -6, -7, 5, 6])
>>> np.take(x, indices)
array([8, 4, 4, 8, 8])
>>> np.take(x, indices, mode="clip")
array([4, 4, 4, 8, 8])

If indices is not one dimensional, the output also has these dimensions.

>>> np.take(x, [[0, 1], [2, 3]])
array([[4, 3],
       [5, 7]])