Bird
0
0

You have a numpy array arr = np.array([0, 1, 2, 3, 4, 5]). How can you use boolean indexing to select only the even numbers?

hard📝 Application Q9 of 15
NumPy - Indexing and Slicing
You have a numpy array arr = np.array([0, 1, 2, 3, 4, 5]). How can you use boolean indexing to select only the even numbers?
Aarr[arr % 2 != 0]
Barr[arr % 2 == 0]
Carr[arr // 2 == 0]
Darr[arr % 2 = 0]
Step-by-Step Solution
Solution:
  1. Step 1: Identify condition for even numbers

    Even numbers have zero remainder when divided by 2: arr % 2 == 0.
  2. Step 2: Use boolean indexing with this condition

    arr[arr % 2 == 0] selects even numbers: 0, 2, 4.
  3. Final Answer:

    arr[arr % 2 == 0] -> Option B
  4. Quick Check:

    Modulo 2 equals zero filters even numbers [OK]
Quick Trick: Use modulo 2 == 0 to filter even numbers [OK]
Common Mistakes:
  • Using single = instead of ==
  • Using floor division instead of modulo
  • Selecting odd numbers by mistake

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes