Boolean indexing for filtering in NumPy - Time & Space Complexity
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?"