Data Science · Level 1 Filtering

NumPy Boolean Masks

Keep only the readings inside a valid range with a boolean mask, and combine conditions the NumPy way.

  • arr > 50 gives True/False values
  • arr[mask] keeps the True ones
  • & and | with parentheses

A comparison gives a mask of True/False; indexing with it keeps the matching values. Trap: combine masks with & and |, each condition in parentheses. and raises an error.

temps = np.array([12, 31, 25, 40])
temps[temps > 24]                   # array([31, 25, 40])
temps[(temps > 24) & (temps < 35)]  # array([31, 25])

Write in_range(values, low, high) that returns the values between low and high, both included, for example in_range(np.array([5, 20, 80]), 10, 50) gives array([20]).

solution.py
Test Suites Pending
Output