Bird
0
0

Which of the following is the correct syntax to use np.where() to select elements from arrays a and b based on condition cond?

easy📝 Syntax Q12 of 15
NumPy - Indexing and Slicing
Which of the following is the correct syntax to use np.where() to select elements from arrays a and b based on condition cond?
Anp.where(a, cond, b)
Bnp.where(cond, a, b)
Cnp.where(a, b, cond)
Dnp.where(cond, b, a)
Step-by-Step Solution
Solution:
  1. Step 1: Recall np.where() parameter order

    The correct order is np.where(condition, x, y) where elements from x are chosen if condition is True, else from y.
  2. Step 2: Match parameters with options

    np.where(cond, a, b) matches this order: np.where(cond, a, b). Others mix the order incorrectly.
  3. Final Answer:

    np.where(cond, a, b) -> Option B
  4. Quick Check:

    np.where(condition, x, y) correct syntax [OK]
Quick Trick: Remember: condition first, then true array, then false array [OK]
Common Mistakes:
  • Swapping the order of arrays and condition
  • Putting arrays before the condition
  • Using np.where() without parentheses

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes