Boolean indexing helps you pick only the data you want from a big list or table. It makes finding and using specific data easy and fast.
Boolean indexing for filtering in NumPy
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
NumPy
filtered_array = array[condition]
The condition is a Boolean array or expression that is the same shape as array.
Only elements where condition is True are kept in filtered_array.
Examples
NumPy
import numpy as np arr = np.array([10, 20, 30, 40, 50]) filtered = arr[arr > 25]
NumPy
arr = np.array([5, 15, 25, 35, 45]) filtered = arr[(arr >= 15) & (arr <= 35)]
NumPy
arr = np.array([1, 2, 3, 4, 5]) filtered = arr[arr % 2 == 0]
Sample Program
This program creates a list of ages and then uses Boolean indexing to select only those 18 or older. It prints both the full list and the filtered list.
NumPy
import numpy as np # Create an array of ages ages = np.array([12, 17, 24, 35, 42, 15, 19]) # Filter ages to get only adults (18 and older) adults = ages[ages >= 18] print("All ages:", ages) print("Adults only:", adults)
Important Notes
Boolean indexing works with arrays of any shape, not just 1D.
Make sure the condition array matches the shape of the original array.
You can combine multiple conditions using & (and), | (or), but use parentheses around each condition.
Summary
Boolean indexing lets you pick data by True/False conditions.
It is a fast and simple way to filter arrays in numpy.
Use logical operators to combine multiple conditions.
Practice
1. What does boolean indexing in numpy allow you to do?
easy
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 AQuick 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
Solution
Step 1: Identify correct boolean indexing syntax
In numpy, filtering usesarr[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 BQuick Check:
Correct syntax is arr[condition] [OK]
Hint: Use arr[condition] to filter arrays in numpy [OK]
Common Mistakes:
- Placing condition outside brackets
- Using non-existent filter method
- Mixing up greater than and less than
3. What is the output of the following code?
import numpy as np arr = np.array([2, 7, 4, 9, 1]) filtered = arr[arr % 2 == 1]
medium
Solution
Step 1: Understand the condition arr % 2 == 1
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 CQuick 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
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 AQuick 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
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.Final Answer:
data[(data % 3 == 0) & (data % 6 != 0)] -> Option DQuick Check:
Use & and parentheses for combined conditions [OK]
Hint: Combine conditions with & and parentheses for filtering [OK]
Common Mistakes:
- Using | instead of & for AND condition
- Mixing up divisibility conditions
- Forgetting parentheses around each condition
