Boolean indexing for filtering in NumPy - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
We want to understand how the time needed to filter data with boolean indexing changes as the data size grows.
How does the filtering step scale when we have more data?
Analyze the time complexity of the following code snippet.
import numpy as np
arr = np.arange(1000)
mask = arr % 2 == 0
filtered = arr[mask]
This code creates an array of numbers, makes a mask for even numbers, and filters the array using that mask.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Checking each element to create the boolean mask and then selecting elements based on that mask.
- How many times: Once for each element in the array (n times).
As the array size grows, the number of checks and selections grows proportionally.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 checks and selections |
| 100 | About 100 checks and selections |
| 1000 | About 1000 checks and selections |
Pattern observation: The work grows directly with the number of elements.
Time Complexity: O(n)
This means the time to filter grows in a straight line as the data size increases.
[X] Wrong: "Filtering with boolean indexing is instant no matter how big the array is."
[OK] Correct: The code must check each element to decide if it matches the condition, so more data means more work.
Understanding how filtering scales helps you explain data processing steps clearly and shows you know how to handle bigger datasets efficiently.
"What if we used multiple conditions combined with & or | in the mask? How would the time complexity change?"
Practice
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]
- Confusing boolean indexing with sorting
- Thinking it changes data types
- Assuming it calculates sums
arr for values greater than 5 using boolean indexing?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]
- Placing condition outside brackets
- Using non-existent filter method
- Mixing up greater than and less than
import numpy as np arr = np.array([2, 7, 4, 9, 1]) filtered = arr[arr % 2 == 1]
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]
- Selecting even numbers instead of odd
- Including all elements without filtering
- Misunderstanding modulo operator
import numpy as np arr = np.array([10, 15, 20, 25]) filtered = arr[arr > 15 and arr < 25]
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]
- Using 'and' instead of '&' in numpy conditions
- Forgetting parentheses around each condition
- Assuming 'or' works like '|'
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?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]
- Using | instead of & for AND condition
- Mixing up divisibility conditions
- Forgetting parentheses around each condition
