You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
## Elegant approach - mask array has the exact same shape as data array
67
+
## Each position containing information about whether that element meets our criteria
65
68
```
66
69
67
70
:::
68
71
69
-
Notice how elegant this approach is: the mask array has the exact same shape as our data array, with each position containing information about whether that element meets our criteria.
70
-
71
72
## Combining Multiple Conditions
72
73
73
74
We can combine multiple conditions using logical operators:
@@ -222,3 +223,126 @@ For large datasets, these techniques are drastically faster than traditional ite
222
223
*`np.isin()` lets us filter based on membership in a set of values
223
224
224
225
:::
226
+
227
+
## Exercises: NumPy Boolean Masking and Advanced Filtering
228
+
229
+
:::{exercise}
230
+
231
+
**Exercise 1 - Basic Boolean Masking:**
232
+
233
+
Create a NumPy array of 20 random integers between 0 and 100. Then:
234
+
235
+
* Create a boolean mask to identify all numbers divisible by 7
236
+
* Use the mask to extract these numbers
237
+
* Count how many numbers are divisible by 7
238
+
239
+
:::
240
+
241
+
:::{solution}
242
+
243
+
```python
244
+
import numpy as np
245
+
246
+
# Create an array of 20 random integers between 0 and 100
247
+
np.random.seed(42) # for reproducibility
248
+
numbers = np.random.randint(0, 101, 20)
249
+
print("Original array:", numbers)
250
+
251
+
# Create a boolean mask for numbers divisible by 7
252
+
mask = numbers %7==0
253
+
print("Boolean mask:", mask)
254
+
255
+
# Extract numbers divisible by 7
256
+
divisible_by_7 = numbers[mask]
257
+
print("Numbers divisible by 7:", divisible_by_7)
258
+
259
+
# Count how many numbers are divisible by 7
260
+
count = np.sum(mask) # True values are treated as 1, False as 0
261
+
print(f"Count of numbers divisible by 7: {count}")
262
+
```
263
+
264
+
:::
265
+
266
+
:::{exercise}
267
+
268
+
**Exercise 2 - Combined Conditions:**
269
+
270
+
Generate a NumPy array of 30 random integers between -50 and 50. Then:
271
+
272
+
Create a mask to find all numbers that are both positive and even
273
+
Create another mask to find all numbers that are either negative or divisible by 5
274
+
Apply both masks to the array and display the results
275
+
276
+
:::
277
+
278
+
:::{solution}
279
+
280
+
:::
281
+
282
+
:::{exercise}
283
+
**Exercise 3 - np.where() for Conditional Assignment:**
284
+
285
+
Create a 4x4 matrix of random integers between 1 and 20. Then:
286
+
287
+
* Use np.where() to replace all odd numbers with -1 while keeping even numbers unchanged
288
+
* Use np.where() again to create a new matrix where:
289
+
* Numbers less than 10 remain the same
290
+
* Numbers between 10 and 15 are replaced with 100
291
+
* Numbers greater than 15 are replaced with 200
292
+
:::
293
+
294
+
:::{solution}
295
+
296
+
```python
297
+
# Create a 4x4 matrix of random integers between 1 and 20
298
+
np.random.seed(42)
299
+
matrix = np.random.randint(1, 21, (4, 4))
300
+
print("Original matrix:")
301
+
print(matrix)
302
+
303
+
# Replace odd numbers with -1, keep even numbers
304
+
odd_replaced = np.where(matrix %2==0, matrix, -1)
305
+
print("\nMatrix with odd numbers replaced by -1:")
0 commit comments