Locate elements and indices using np.where(), np.nonzero(), and np.searchsorted().
`np.where(condition, [x, y])` returns indices where condition is True, or performs ternary `if-else` element selection across arrays.
`np.where(condition, x, y)` is a vectorized ternary operator: 'If condition is True pick from x, otherwise pick from y'.
1. `np.where(cond)`: Returns a tuple of coordinate index arrays. 2. `np.where(cond, x, y)`: Vectorized if-else replacement.
idx = np.where(arr == 30); new_arr = np.where(arr < 0, 0, arr)idx = np.where(arr > 20); print(idx) # Returns tuple: (array([1, 2]),)idx = np.where(arr > 20)[0] # Extract the 1D index array`np.where(condition)` returns a tuple containing index arrays for each dimension.
Solve the objective below using NumPy vectorized syntax
Given `arr = np.array([-5, 10, -15, 20])`, use `np.where(arr < 0, 0, arr)` and print the result.
[ 0 10 0 20]