Combining conditions in NumPy - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
We want to see how the time needed changes when we combine conditions in numpy arrays.
How does checking multiple conditions together affect the work done?
Analyze the time complexity of the following code snippet.
import numpy as np
arr = np.random.randint(0, 100, size=1000)
result = (arr > 20) & (arr < 80)
filtered = arr[result]
This code creates an array, checks which elements are between 20 and 80, and selects those elements.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Checking each element against two conditions and combining results.
- How many times: Once for each element in the array (n times).
As the array gets bigger, the number of checks grows in direct proportion.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 20 checks (2 per element) |
| 100 | About 200 checks |
| 1000 | About 2000 checks |
Pattern observation: The work doubles if the input size doubles because each element is checked twice.
Time Complexity: O(n)
This means the time grows in a straight line with the number of elements checked.
[X] Wrong: "Combining two conditions makes the code run twice as slow in a complex way."
[OK] Correct: Actually, each element is checked a fixed number of times, so the time grows simply with the number of elements, not in a complicated way.
Understanding how combining conditions affects time helps you write clear and efficient data checks in real projects.
What if we used three conditions combined with & instead of two? How would the time complexity change?
Practice
a > 5 and b < 10 in NumPy to select elements where both are true?Solution
Step 1: Understand combining conditions in NumPy
NumPy requires each condition to be in parentheses when using&(AND) or|(OR) operators.Step 2: Identify correct syntax for AND condition
The correct way to combinea > 5andb < 10with AND is(a > 5) & (b < 10).Final Answer:
Use(a > 5) & (b < 10)-> Option AQuick Check:
Parentheses + & = correct AND condition [OK]
- Omitting parentheses around conditions
- Using Python 'and' instead of '&' for arrays
- Using 'or' instead of '|' for arrays
arr where values are NOT equal to 0 and less than 10?Solution
Step 1: Use parentheses for each condition
Each condition must be enclosed in parentheses:(arr != 0)and(arr < 10).Step 2: Combine with AND operator
Use&to combine conditions for selecting elements satisfying both.Final Answer:
arr[(arr != 0) & (arr < 10)] -> Option BQuick Check:
Parentheses + & + correct conditions = syntax correct [OK]
- Missing parentheses causing syntax errors
- Using bitwise NOT (~) incorrectly
- Using Python 'and' instead of '&'
import numpy as np arr = np.array([1, 5, 8, 12, 3, 7]) result = arr[(arr > 3) | (arr == 1)] print(result)
What is the output?
Solution
Step 1: Evaluate each condition on the array
arr > 3is True for 5, 8, 12, 7;arr == 1is True for 1.Step 2: Combine conditions with OR operator
Elements where either condition is True are selected: 1, 5, 8, 12, 7.Final Answer:
[1 5 8 12 7] -> Option DQuick Check:
OR condition selects 1 and all >3 values [OK]
- Forgetting to include elements equal to 1
- Using AND (&) instead of OR (|)
- Misreading the array values
import numpy as np arr = np.array([2, 4, 6, 8]) filtered = arr[arr > 3 && arr < 8] print(filtered)
Solution
Step 1: Identify operator error
NumPy uses bitwise operators '&' and '|' for element-wise logical operations, not '&&'.Step 2: Correct operator usage
Replace '&&' with '&' and add parentheses around each condition.Final Answer:
Using '&&' instead of '&' for combining conditions -> Option CQuick Check:
'&&' is invalid in NumPy, use '&' with parentheses [OK]
- Using '&&' from other languages
- Not adding parentheses around conditions
- Using Python 'and' instead of '&'
data = np.array([10, 15, 20, 25, 30, 35]). You want to select elements that are either less than 20 or greater than or equal to 30, but NOT equal to 15. Which code correctly filters data?Solution
Step 1: Combine OR conditions inside parentheses
Use(data < 20) | (data >= 30)to select elements less than 20 or greater or equal to 30.Step 2: Exclude elements equal to 15 with AND
Combine with& (data != 15)to exclude 15.Final Answer:
data[((data < 20) | (data >= 30)) & (data != 15)] -> Option AQuick Check:
Parentheses + OR + AND + NOT = correct filter [OK]
- Missing parentheses causing wrong precedence
- Using AND instead of OR for first condition
- Incorrect negation of 15
