Tsallaka zuwa abun ciki
Justice Tech School

Boolean Indexing

A boolean mask selects the True items.

Example
a = np.array([5, 12, 7, 20, 3])
print(a > 6)
print(a[a > 6])
print(a[(a > 4) & (a < 15)])
print((a > 6).sum())

Result

[False  True  True  True False]
[12  7 20]
[ 5 12  7]
3

Exercise

Mask.

a[a  6]

Quiz Test

Which combines masks?

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