Boolean indexing in NumPy - Time & Space Complexity
We want to understand how the time it takes to select data using boolean indexing changes as the data size grows.
How does the work grow when we filter arrays with conditions?
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, makes a mask for even numbers, and selects those numbers using boolean indexing.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Checking each element against the condition (arr % 2 == 0) and then selecting elements based on the mask.
- How many times: Once for each element in the array, so n times where n is the array size.
As the array size grows, the number of checks and selections grows roughly the same way.
| 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 size of the input array.
Time Complexity: O(n)
This means the time to filter grows in a straight line as the array gets bigger.
[X] Wrong: "Boolean indexing is instant no matter the array size because it just picks elements."
[OK] Correct: Actually, it must check each element to decide if it fits the condition, so it takes longer with bigger arrays.
Understanding how filtering data scales helps you write efficient code and explain your choices clearly in real projects and interviews.
"What if we used multiple conditions combined with & or | in the mask? How would the time complexity change?"
Practice
numpy allow you to do?Solution
Step 1: Understand boolean indexing concept
Boolean indexing uses a True/False array to pick elements from another array.Step 2: Identify the correct operation
Only Select elements from an array using a True/False mask describes selecting elements based on True/False values.Final Answer:
Select elements from an array using a True/False mask -> Option DQuick Check:
Boolean indexing = Select elements [OK]
- Confusing boolean indexing with sorting
- Thinking it changes data types
- Assuming it creates new arrays with zeros
arr using boolean indexing?Solution
Step 1: Understand boolean indexing syntax
The correct syntax is to use the array with a boolean mask inside square brackets: arr[condition].Step 2: Check each option
arr[arr > 5] uses arr[arr > 5], which is correct. Others have wrong order or invalid methods.Final Answer:
arr[arr > 5] -> Option CQuick Check:
Correct syntax = arr[condition] [OK]
- Placing condition before array name
- Using invalid methods like select()
- Using wrong comparison operators
import numpy as np arr = np.array([2, 7, 4, 9, 1]) result = arr[arr % 2 == 1]
Solution
Step 1: Understand the condition arr % 2 == 1
This condition selects elements where the remainder when divided by 2 is 1, i.e., odd numbers.Step 2: Apply condition to array elements
Elements 7, 9, and 1 are odd, so they are selected.Final Answer:
[7 9 1] -> Option AQuick Check:
Odd elements = [7 9 1] [OK]
- Selecting even numbers instead
- Confusing modulo operator
- Returning full array instead of filtered
import numpy as np arr = np.array([1, 2, 3, 4]) mask = arr > 2 print(arr[mask == True])
Solution
Step 1: Analyze the boolean mask creation
mask = arr > 2 creates a boolean array correctly.Step 2: Check the indexing expression
Using mask == True is redundant since mask is already boolean. It's unnecessary and bad practice.Step 3: Correct usage
Use arr[mask] directly without comparing mask to True.Final Answer:
mask == True is unnecessary and redundant -> Option AQuick Check:
Use boolean mask directly [OK]
- Adding '== True' to boolean mask
- Trying to convert numpy array to list for indexing
- Misunderstanding comparison operators
data = np.array([10, 15, 20, 25, 30]), how can you select elements greater than 12 and less than 28 using boolean indexing?Solution
Step 1: Understand combining conditions with & and parentheses
In numpy, use & for element-wise AND and wrap each condition in parentheses.Step 2: Check each option
data[(data > 12) & (data < 28)]correctly uses & (element-wise AND) with parentheses around each condition. Using Python keywordsand/orcause errors because they don't work element-wise on arrays. Using|(bitwise OR) selects elements greater than 12 or less than 28, which is wrong here.Final Answer:
data[(data > 12) & (data < 28)] -> Option BQuick Check:
Use & with parentheses for AND conditions [OK]
- Using 'and' or 'or' instead of & or |
- Not using parentheses around conditions
- Using | (OR) instead of & (AND) for filtering
Ready to prove it?
Pass the test and this pattern moves unknown
