Why boolean masking matters in NumPy - Performance Analysis
Start learning this pattern below
Jump into concepts and practice - no test required
We want to see how fast numpy handles selecting data using boolean masks.
How does the time to pick items grow when the data gets bigger?
Analyze the time complexity of the following code snippet.
import numpy as np
arr = np.arange(1000000)
mask = arr % 2 == 0
filtered = arr[mask]
This code creates a large array, makes a mask for even numbers, and selects those numbers.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Checking each element to see if it is even (creating the mask).
- How many times: Once for every element in the array.
- Secondary operation: Using the mask to pick elements (also touches each element once).
As the array gets bigger, the time to check and select grows in a straight line.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 checks and 10 picks |
| 100 | About 100 checks and 100 picks |
| 1000 | About 1000 checks and 1000 picks |
Pattern observation: The work grows directly with the number of items.
Time Complexity: O(n)
This means the time to filter grows in a straight line as the data size grows.
[X] Wrong: "Boolean masking is instant no matter how big the data is."
[OK] Correct: The mask must check every item, so bigger data means more work and more time.
Understanding how boolean masking scales helps you explain data filtering clearly and confidently in real tasks.
"What if we used multiple conditions combined in the mask? How would the time complexity change?"
Practice
numpy?Solution
Step 1: Understand boolean masking concept
Boolean masking uses a True/False array to pick elements from another array.Step 2: Identify the main use
This helps select only the elements where the mask is True, filtering data easily.Final Answer:
To select elements from an array based on True/False conditions -> Option BQuick Check:
Boolean mask = select elements [OK]
- Thinking it sorts the array
- Confusing masking with data type change
- Assuming it fills arrays with zeros
arr to select values greater than 5?Solution
Step 1: Understand comparison operators
To select values greater than 5, use the greater than operator:>.Step 2: Check syntax correctness
mask = arr > 5creates a boolean array where True means element > 5.Final Answer:
mask = arr > 5 -> Option AQuick Check:
Use > for greater than [OK]
- Using single equals (=) instead of comparison (>)
- Using < instead of >
- Using == which checks equality, not greater than
import numpy as np arr = np.array([2, 7, 4, 9, 1]) mask = arr > 4 result = arr[mask]
What is the value of
result?Solution
Step 1: Create boolean mask for elements > 4
Elements greater than 4 are 7 and 9, so mask is [False, True, False, True, False].Step 2: Apply mask to array
Usingarr[mask]selects elements where mask is True: [7, 9].Final Answer:
[7, 9] -> Option DQuick Check:
Mask picks elements > 4 [OK]
- Including elements not > 4
- Confusing mask with index positions
- Selecting elements less than or equal to 4
import numpy as np arr = np.array([1, 3, 5, 7]) mask = arr > 4 print(arr[mask])
It raises an error. Why?
Solution
Step 1: Check mask creation
The maskarr > 4creates a boolean array of same length as arr without error.Step 2: Check indexing with mask
Usingarr[mask]selects elements > 4 without error.Final Answer:
The mask is created correctly; no error occurs -> Option AQuick Check:
Correct mask syntax means no error [OK]
- Confusing assignment (=) with comparison (>)
- Assuming mask length mismatch error
- Thinking non-numeric values cause error here
data = np.array([10, 0, 5, -3, 8]). You want to select only positive numbers excluding zero using boolean masking. Which code correctly achieves this?Solution
Step 1: Define condition for positive numbers excluding zero
Positive numbers are greater than zero, so condition isdata > 0.Step 2: Apply mask and select elements
Usingdata[data > 0]selects 10, 5, and 8, excluding zero and negatives.Final Answer:
mask = data > 0 result = data[mask] -> Option CQuick Check:
Use > 0 to exclude zero and negatives [OK]
- Using >= 0 includes zero
- Using != 0 includes negatives
- Using < 0 selects negatives, not positives
