</>
Topic 2 of 3
Theory

Indexes and masks

Comparing an array gives a mask — an array of True/False. Use a mask to pick elements:

a = np.array([3, -1, 7, -5])
print(a > 0)        # [ True False  True False]
print(a[a > 0])     # [3 7]
a[a < 0] = 0        # replace negatives with zero

np.clip(a, lo, hi) trims values at the edges, np.convolve slides a window over an array.

Positives only

Easy+10 XP

Return only the positive elements of the array.

positives(np.array([3, -1, 7])) → [3 7]
main.pyLoading Python, numpy…
Output
Press "Check" to run the tests