dpnp.choose

dpnp.choose(a, choices, out=None, mode='wrap')[source]

Construct an array from an index array and a set of arrays to choose from.

For full documentation refer to numpy.choose.

Parameters:
  • a ({dpnp.ndarray, usm_ndarray}) -- An integer array of indices indicating the position of the array in choices to choose from. Behavior of out-of-bounds integers (i.e., integers outside of [0, n-1] where n is the number of choices) is determined by the mode keyword.

  • choices ({dpnp.ndarray, usm_ndarray, sequence of dpnp.ndarrays and usm_ndarrays}) -- Choice arrays. a and choice arrays must be broadcast-compatible. If choices is an array, the array is unstacked into a sequence of arrays.

  • out ({None, dpnp.ndarray, usm_ndarray}, optional) --

    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 merged result.

Return type:

dpnp.ndarray

See also

dpnp.ndarray.choose

Equivalent method.

dpnp.take_along_axis

Preferable if choices is an array.

Examples

>>> import dpnp as np
>>> choices = np.array([[0, 1, 2, 3], [10, 11, 12, 13],
...   [20, 21, 22, 23], [30, 31, 32, 33]])
>>> np.choose(np.array([2, 3, 1, 0]), choices
... # the first element of the result will be the first element of the
... # third (2+1) "array" in choices, namely, 20; the second element
... # will be the second element of the fourth (3+1) choice array, i.e.,
... # 31, etc.
... )
array([20, 31, 12, 3])
>>> np.choose(np.array([2, 4, 1, 0]), choices, mode='clip'
... # 4 goes to 3 (4-1)
... )
array([20, 31, 12, 3])
>>> # because there are 4 choice arrays
>>> np.choose(np.array([2, 4, 1, 0]), choices, mode='wrap'
... # 4 is clipped to 3
... )
array([20, 31, 12, 3])
>>> np.choose(np.array([2, -1, 1, 0]), choices, mode='wrap'
... # -1 goes to 3 (-1+4)
... )
array([20, 31, 12, 3])

An example using broadcasting:

>>> a = np.array([[1, 0, 1], [0, 1, 0], [1, 0, 1]])
>>> choices = np.array([-10, 10])
>>> np.choose(a, choices)
array([[ 10, -10,  10],
       [-10,  10, -10],
       [ 10, -10,  10]])