Block 15: Boolean Masks & Fancy Indexing
Filter arrays using conditions; use index arrays.
Concepts
- Boolean condition produces a mask: arr > 5
- arr[mask] to filter
- Combining conditions with & and |
- np.where() for conditional selection
Code Examples
See exercise below.
Exercise
From 0–99, extract: values between 20–50, values NOT divisible by 3. Replace all values below 10 with 0 in a random array.
Homework
Use np.where to create a new array where positive values stay, negative become -1.