np.where() in python code example
Example 1: np.where
>>> 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])
Example 2: numpy where
import numpy as np
a = np.arange(10)
single_where = np.where((a<5),-1,a)
multiple_where = np.where((a<5),-1,np.where((a>5),0,a))
Example 3: numpy.where
Parameters:
condition : When True, yield x, otherwise yield y.
x, y : Values from which to choose. x, y and condition need to be broadcastable to some shape.
Returns:
out : [ndarray or tuple of ndarrays] If both x and y are specified, the output array contains elements of x where condition is True, and elements from y elsewhere.