np.where() for conditional selection in NumPy - Time & Space Complexity
We want to understand how the time to run np.where() changes as the size of the input array grows.
Specifically, how does the work inside np.where() scale with bigger arrays?
Analyze the time complexity of the following code snippet.
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
result = np.where(arr > 3, arr, 0)
This code checks each element in arr. If the element is greater than 3, it keeps it; otherwise, it replaces it with 0.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Checking each element of the array against the condition.
- How many times: Once for every element in the array.
As the array gets bigger, the number of checks grows directly with the number of elements.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 checks |
| 100 | About 100 checks |
| 1000 | About 1000 checks |
Pattern observation: The work grows in a straight line with the input size.
Time Complexity: O(n)
This means the time to run np.where() grows directly in proportion to the number of elements in the array.
[X] Wrong: "np.where() runs in constant time no matter the array size."
[OK] Correct: The function must check each element to decide what to output, so it takes longer with bigger arrays.
Understanding how array operations scale helps you write efficient code and explain your choices clearly in real projects or interviews.
"What if we used np.where() on a 2D array instead of a 1D array? How would the time complexity change?"
Practice
np.where() function do in NumPy?Solution
Step 1: Understand the purpose of np.where()
np.where()is used to choose elements from two arrays depending on a condition.Step 2: Compare with other options
Sorting, calculating mean, or creating zeros are different functions likenp.sort(),np.mean(), andnp.zeros().Final Answer:
Selects elements from two arrays based on a condition -> Option AQuick Check:
np.where() = conditional selection [OK]
- Confusing np.where() with sorting or aggregation functions
- Thinking np.where() creates new arrays without conditions
- Assuming np.where() only finds indices, not selects values
np.where() to select elements from arrays a and b based on condition cond?Solution
Step 1: Recall np.where() parameter order
The correct order isnp.where(condition, x, y)where elements fromxare chosen if condition is True, else fromy.Step 2: Match parameters with options
np.where(cond, a, b) matches this order:np.where(cond, a, b). Others mix the order incorrectly.Final Answer:
np.where(cond, a, b) -> Option BQuick Check:
np.where(condition, x, y) correct syntax [OK]
- Swapping the order of arrays and condition
- Putting arrays before the condition
- Using np.where() without parentheses
import numpy as np arr = np.array([10, 20, 30, 40]) result = np.where(arr > 25, arr, 0) print(result)
Solution
Step 1: Evaluate the condition arr > 25
Elements greater than 25 are 30 and 40 at indices 2 and 3.Step 2: Apply np.where() selection
For elements > 25, keep original value; else replace with 0. So result is [0, 0, 30, 40].Final Answer:
[ 0 0 30 40] -> Option AQuick Check:
Condition true keeps value, false replaces with 0 [OK]
- Confusing which elements satisfy the condition
- Mixing up true and false array values
- Forgetting np.where returns a new array
import numpy as np x = np.array([1, 2, 3, 4]) y = np.array([10, 20, 30, 40]) result = np.where(x > 2, y) print(result)
Solution
Step 1: Check np.where() arguments
np.where() requires three arguments for conditional selection: condition, array if True, array if False.Step 2: Identify missing argument
The code only provides condition and one array, missing the third argument for false case.Final Answer:
Missing third argument for false condition in np.where() -> Option CQuick Check:
np.where needs condition, true array, false array [OK]
- Using np.where with only two arguments for selection
- Assuming np.where can infer missing arrays
- Confusing np.where with np.nonzero
a = np.array([5, 15, 25, 35]) and b = np.array([100, 200, 300, 400]), how can you create a new array that takes elements from b when a is greater than 20, otherwise from a, using np.where()?Solution
Step 1: Understand the condition and selection
We want elements frombwherea > 20, else froma.Step 2: Apply np.where() with correct order
Usenp.where(condition, x, y)wherexis chosen if condition True, elsey. Sonp.where(a > 20, b, a).Final Answer:
np.where(a > 20, b, a) -> Option DQuick Check:
Condition true picks b, false picks a [OK]
- Swapping arrays in np.where() parameters
- Using wrong comparison operator
- Confusing which array to pick on true condition
Ready to prove it?
Pass the test and this pattern moves unknown
