Tsallaka zuwa abun ciki
Justice Tech School

where, select and clip

np.where picks between two values by condition. clip limits values to a range and select handles several conditions.

Example
import numpy as np
a = np.array([-2, 5, 11])
print(np.where(a > 0, 'pos', 'neg'))
print(np.clip(a, 0, 10))
print(np.select([a < 0, a < 10], ['low', 'mid'], default='high'))

Result

['neg' 'pos' 'pos']
[ 0  5 10]
['low' 'mid' 'high']

Exercise

Limit to 0 and 10.

np.(a, 0, 10)

Quiz Test

What does np.where(c, x, y) do?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.