dpnp.put_along_axis

dpnp.put_along_axis(a, ind, values, axis, mode='wrap')[source]

Put values into the destination array by matching 1d index and data slices.

This iterates over matching 1d slices oriented along the specified axis in the index and data arrays, and uses the former to place values into the latter. These slices can be different lengths.

Functions returning an index along an axis, like dpnp.argsort and dpnp.argpartition, produce suitable indices for this function.

For full documentation refer to numpy.put_along_axis.

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

  • ind ({dpnp.ndarray, usm_ndarray}, (Ni..., J, Nk...)) -- Indices to change along each 1d slice of a. This must match the dimension of input array, but dimensions in Ni and Nj may be 1 to broadcast against a.

  • values ({scalar, array_like}, (Ni..., J, Nk...)) -- Values to insert at those indices. Its shape and dimension are broadcast to match that of ind.

  • axis ({None, int}) -- The axis to take 1d slices along. If axis is None, the destination array is treated as if a flattened 1d view had been created of it.

  • 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".

See also

dpnp.put

Put values along an axis, using the same indices for every 1d slice.

dpnp.take_along_axis

Take values from the input array by matching 1d index and data slices.

Examples

For this sample array

>>> import dpnp as np
>>> a = np.array([[10, 30, 20], [60, 40, 50]])

We can replace the maximum values with:

>>> ai = np.argmax(a, axis=1, keepdims=True)
>>> ai
array([[1],
       [0]])
>>> np.put_along_axis(a, ai, 99, axis=1)
>>> a
array([[10, 99, 20],
       [99, 40, 50]])