Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Boolean indexing for filtering
📖 Scenario: Imagine you have a list of daily temperatures recorded in a city. You want to find out which days were warm, meaning the temperature was above 20 degrees Celsius.
🎯 Goal: You will create a NumPy array of temperatures, set a threshold for warm days, use boolean indexing to filter the warm days, and then display those warm temperatures.
📋 What You'll Learn
Create a NumPy array called temperatures with the exact values: 15, 22, 19, 24, 18, 30, 21
Create a variable called warm_threshold and set it to 20
Use boolean indexing with temperatures and warm_threshold to create a new array called warm_days containing only temperatures above the threshold
Print the warm_days array
💡 Why This Matters
🌍 Real World
Filtering data based on conditions is common in weather analysis, finance, and health monitoring to focus on important values.
💼 Career
Data scientists often use boolean indexing to quickly select and analyze subsets of data without loops, making their code faster and cleaner.
Progress0 / 4 steps
1
Create the temperatures array
Import NumPy as np and create a NumPy array called temperatures with these exact values: 15, 22, 19, 24, 18, 30, 21
NumPy
Hint
Use np.array([...]) to create the array with the exact numbers.
2
Set the warm day threshold
Create a variable called warm_threshold and set it to the number 20
NumPy
Hint
Just assign the number 20 to the variable warm_threshold.
3
Filter warm days using boolean indexing
Use boolean indexing with temperatures and warm_threshold to create a new NumPy array called warm_days that contains only the temperatures greater than warm_threshold
NumPy
Hint
Use temperatures > warm_threshold inside the square brackets to filter.
4
Print the warm days
Print the warm_days array to display the temperatures above the threshold
NumPy
Hint
Use print(warm_days) to show the filtered temperatures.
Practice
(1/5)
1. What does boolean indexing in numpy allow you to do?
easy
A. Select elements from an array based on True/False conditions
B. Sort an array in ascending order
C. Change the data type of an array
D. Calculate the sum of all elements in an array
Solution
Step 1: Understand boolean indexing concept
Boolean indexing uses a True/False array to pick elements from another array.
Step 2: Compare with other options
Sorting, changing data type, and summing are different numpy operations, not boolean indexing.
Final Answer:
Select elements from an array based on True/False conditions -> Option A
Quick Check:
Boolean indexing = filtering by True/False [OK]
Hint: Boolean indexing picks elements where condition is True [OK]
Common Mistakes:
Confusing boolean indexing with sorting
Thinking it changes data types
Assuming it calculates sums
2. Which of the following is the correct syntax to filter array arr for values greater than 5 using boolean indexing?
easy
A. arr > 5[arr]
B. arr[arr > 5]
C. arr.filter(arr > 5)
D. arr[arr < 5]
Solution
Step 1: Identify correct boolean indexing syntax
In numpy, filtering uses arr[condition] where condition is a boolean array.
Step 2: Check each option
arr[arr > 5] uses correct syntax. arr > 5[arr] is invalid syntax. arr.filter(arr > 5) is not a numpy method. arr[arr < 5] filters for less than 5, not greater.
Final Answer:
arr[arr > 5] -> Option B
Quick Check:
Correct syntax is arr[condition] [OK]
Hint: Use arr[condition] to filter arrays in numpy [OK]
This condition selects odd numbers because odd numbers have remainder 1 when divided by 2.
Step 2: Apply condition to array elements
Elements 7, 9, and 1 are odd, so they are selected.
Final Answer:
[7 9 1] -> Option C
Quick Check:
Filter odd numbers = [7 9 1] [OK]
Hint: Use modulo (%) to filter odd/even numbers [OK]
Common Mistakes:
Selecting even numbers instead of odd
Including all elements without filtering
Misunderstanding modulo operator
4. The following code throws an error. What is the mistake?
import numpy as np
arr = np.array([10, 15, 20, 25])
filtered = arr[arr > 15 and arr < 25]
medium
A. Using 'and' instead of '&' for element-wise condition
B. Missing parentheses around conditions
C. Using 'or' instead of 'and'
D. Array is not defined properly
Solution
Step 1: Identify boolean operator error
In numpy, element-wise logical operations require '&' instead of Python's 'and'.
Step 2: Understand why 'and' causes error
'and' expects single boolean, but arr > 15 and arr < 25 returns arrays, causing TypeError.
Final Answer:
Using 'and' instead of '&' for element-wise condition -> Option A
Quick Check:
Use '&' for element-wise logical AND [OK]
Hint: Use & with parentheses for multiple conditions [OK]
Common Mistakes:
Using 'and' instead of '&' in numpy conditions
Forgetting parentheses around each condition
Assuming 'or' works like '|'
5. Given a numpy array data = np.array([3, 6, 9, 12, 15, 18]), how would you filter values that are divisible by 3 but not by 6 using boolean indexing?
hard
A. data[(data % 3 == 0) & (data % 6 == 0)]
B. data[(data % 3 == 0) | (data % 6 != 0)]
C. data[(data % 3 != 0) & (data % 6 == 0)]
D. data[(data % 3 == 0) & (data % 6 != 0)]
Solution
Step 1: Define conditions for filtering
We want numbers divisible by 3 (data % 3 == 0) but not divisible by 6 (data % 6 != 0).
Step 2: Combine conditions with element-wise AND
Use '&' to combine both conditions inside parentheses for correct boolean indexing.
Step 3: Apply combined condition to data array
data[(data % 3 == 0) & (data % 6 != 0)] correctly applies both conditions with '&'. Others use wrong operators or conditions.