Bird
0
0

Given a NumPy array arr = np.array([1, 2, 3, 4, 5]), how can you create a new array containing only the even numbers?

hard📝 Application Q9 of 15
NumPy - Fundamentals
Given a NumPy array arr = np.array([1, 2, 3, 4, 5]), how can you create a new array containing only the even numbers?
AUse boolean indexing: arr[arr % 2 == 0]
BUse arr.filter(even)
CUse arr.select(2)
DUse arr.where(even)
Step-by-Step Solution
Solution:
  1. Step 1: Understand boolean indexing

    NumPy allows filtering arrays using conditions inside square brackets.
  2. Step 2: Apply condition for even numbers

    arr % 2 == 0 creates a boolean mask for even elements.
  3. Final Answer:

    Use boolean indexing: arr[arr % 2 == 0] -> Option A
  4. Quick Check:

    Boolean indexing filters arrays [OK]
Quick Trick: Filter arrays with conditions inside brackets [OK]
Common Mistakes:
  • Using non-existent methods like filter or select
  • Confusing syntax for filtering
  • Trying to use .where incorrectly

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes