Bird
0
0

Given two arrays a = np.array([1, 2, 3]) and b = np.array([4, 5, 6]), how can you use np.where() to create an array that takes elements from a if they are even, otherwise from b?

hard📝 Application Q9 of 15
NumPy - Indexing and Slicing
Given two arrays a = np.array([1, 2, 3]) and b = np.array([4, 5, 6]), how can you use np.where() to create an array that takes elements from a if they are even, otherwise from b?
Anp.where(a % 2 == 0, a, b)
Bnp.where(b % 2 == 0, a, b)
Cnp.where(a % 2 != 0, a, b)
Dnp.where(a == b, a, b)
Step-by-Step Solution
Solution:
  1. Step 1: Identify condition for even elements in a

    Condition is a % 2 == 0 to check even numbers in a.
  2. Step 2: Use np.where to select from a or b

    Use np.where(a % 2 == 0, a, b) to pick from a if even, else from b.
  3. Final Answer:

    np.where(a % 2 == 0, a, b) -> Option A
  4. Quick Check:

    np.where(condition, a, b) picks based on condition [OK]
Quick Trick: Condition on a, pick from a or b accordingly [OK]
Common Mistakes:
  • Checking condition on wrong array
  • Using != instead of == for even check
  • Confusing which array to pick from

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes