mindspore.numpy.where

View Source On Gitee
mindspore.numpy.where(condition, x=None, y=None)[source]

Returns elements chosen from x or y depending on condition.

Note

As nonzero is not supported, both x and y must be provided Tensor

input.

Parameters
  • condition (Tensor) – where True, yield x, otherwise yield y.

  • x (Tensor) – Values from which to choose. Default: None .

  • y (Tensor) – Values from which to choose. x, y and condition need to be broadcastable to some shape. Default: None .

Returns

Tensor or scalar, with elements from x where condition is True, and elements from y elsewhere.

Raises

ValueError – If operands cannot be broadcast.

Supported Platforms:

Ascend GPU CPU

Examples

>>> import mindspore.numpy as np
>>> condition = np.full((1, 1, 2), [False, True])
>>> x = np.full((1, 3, 2), 5)
>>> y = np.full((2, 1, 1), 7)
>>> output = np.where(condition, x, y)
>>> print(output)
[[[7 5]
[7 5]
[7 5]]
[[7 5]
[7 5]
[7 5]]]