Hint: np.where needs three arguments for selection [OK]
Common Mistakes:
Using np.where with only two arguments for selection
Assuming np.where can infer missing arrays
Confusing np.where with np.nonzero
5. Given arrays 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()?
hard
A. np.where(a > 20, a, b)
B. np.where(b > 20, a, b)
C. np.where(a < 20, b, a)
D. np.where(a > 20, b, a)
Solution
Step 1: Understand the condition and selection
We want elements from b where a > 20, else from a.
Step 2: Apply np.where() with correct order
Use np.where(condition, x, y) where x is chosen if condition True, else y. So np.where(a > 20, b, a).
Final Answer:
np.where(a > 20, b, a) -> Option D
Quick Check:
Condition true picks b, false picks a [OK]
Hint: Condition first, then true array, then false array [OK]
Common Mistakes:
Swapping arrays in np.where() parameters
Using wrong comparison operator
Confusing which array to pick on true condition
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.