dpnp.where

dpnp.where(condition, x=None, y=None, /, *, order='K', out=None)[source]

Return elements chosen from x or y depending on condition.

When only condition is provided, this function is a shorthand for dpnp.nonzero(condition).

For full documentation refer to numpy.where.

Parameters:
  • condition ({dpnp.ndarray, usm_ndarray}) -- When True, yield x, otherwise yield y.

  • x ({dpnp.ndarray, usm_ndarray, scalar}, optional) -- Values from which to choose. x, y and condition need to be broadcastable to some shape.

  • y ({dpnp.ndarray, usm_ndarray, scalar}, optional) -- Values from which to choose. x, y and condition need to be broadcastable to some shape.

  • order ({"K", "C", "F", "A"}, optional) -- Memory layout of the new output array, if keyword out is None. Default: "K".

  • out ({None, dpnp.ndarray, usm_ndarray}, optional) -- The array into which the result is written. The data type of out must match the expected shape and the expected data type of the result. If None then a new array is returned. Default: None.

Returns:

y -- An array with elements from x when condition is True, and elements from y elsewhere.

Return type:

dpnp.ndarray

See also

dpnp.choose

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

dpnp.nonzero

Return the indices of the elements that are non-zero.

Examples

>>> import dpnp as np
>>> a = np.arange(10)
>>> a
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> np.where(a < 5, a, 10*a)
array([ 0,  1,  2,  3,  4, 50, 60, 70, 80, 90])

This can be used on multidimensional arrays too:

>>> np.where(np.array([[True, False], [True, True]]),
...          np.array([[1, 2], [3, 4]]),
...          np.array([[9, 8], [7, 6]]))
array([[1, 8],
       [3, 4]])

The shapes of x, y, and the condition are broadcast together:

>>> x, y = np.ogrid[:3, :4]
>>> np.where(x < y, x, 10 + y)  # both x and 10+y are broadcast
array([[10,  0,  0,  0],
       [10, 11,  1,  1],
       [10, 11, 12,  2]])
>>> a = np.array([[0, 1, 2],
...               [0, 2, 4],
...               [0, 3, 6]])
>>> np.where(a < 4, a, -1)  # -1 is broadcast
array([[ 0,  1,  2],
       [ 0,  2, -1],
       [ 0,  3, -1]])