Bird
0
0

Given a 2D numpy array arr = np.array([[1, 2], [3, 4], [5, 6]]), how would you select all elements greater than 3 using boolean indexing?

hard📝 Application Q8 of 15
NumPy - Indexing and Slicing
Given a 2D numpy array arr = np.array([[1, 2], [3, 4], [5, 6]]), how would you select all elements greater than 3 using boolean indexing?
Aarr[arr > 3]
Barr[:, arr > 3]
Carr[arr < 3]
Darr[3]
Step-by-Step Solution
Solution:
  1. Step 1: Understand boolean indexing on 2D arrays

    Boolean indexing flattens and selects elements matching condition.
  2. Step 2: Apply condition arr > 3

    arr[arr > 3] selects elements greater than 3: 4, 5, 6.
  3. Final Answer:

    arr[arr > 3] -> Option A
  4. Quick Check:

    Boolean indexing works element-wise on 2D arrays [OK]
Quick Trick: Boolean indexing flattens 2D arrays when filtering [OK]
Common Mistakes:
  • Using arr[:, arr > 3] which is invalid
  • Selecting elements less than 3
  • Trying to index with a single integer

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes