bundles / numpy 2.4.3 / numpy / where
_ArrayFunctionDispatcher
numpy:where
Summary
Return elements chosen from x or y depending on condition.
Extended Summary
Parameters
condition: array_like, boolWhere True, yield x, otherwise yield y.
x, y: array_likeValues from which to choose. x, y and condition need to be broadcastable to some shape.
Returns
out: ndarrayAn array with elements from x where condition is True, and elements from y elsewhere.
Notes
If all the arrays are 1-D, where is equivalent to
[xv if c else yv for c, xv, yv in zip(condition, x, y)]
Examples
import numpy as np a = np.arange(10) a np.where(a < 5, a, 10*a)✓
np.where([[True, False], [True, True]], [[1, 2], [3, 4]], [[9, 8], [7, 6]])✓
x, y = np.ogrid[:3, :4] np.where(x < y, x, 10 + y) # both x and 10+y are broadcast✓
a = np.array([[0, 1, 2], [0, 2, 4], [0, 3, 6]]) np.where(a < 4, a, -1) # -1 is broadcast✓
See also
- choose
- nonzero
The function that is called when x and y are omitted
Aliases
-
numpy.where