Bird
0
0

Why does boolean indexing in numpy return a 1D array even when applied to a 2D array?

hard📝 Conceptual Q10 of 15
NumPy - Indexing and Slicing
Why does boolean indexing in numpy return a 1D array even when applied to a 2D array?
ABecause boolean indexing flattens the array and returns matching elements in 1D
BBecause numpy does not support boolean indexing on 2D arrays
CBecause the original array is converted to a list first
DBecause the condition must be 1D for boolean indexing
Step-by-Step Solution
Solution:
  1. Step 1: Understand boolean indexing behavior on multi-dimensional arrays

    Boolean indexing selects elements matching condition and returns them flattened.
  2. Step 2: Check options for correct explanation

    Because boolean indexing flattens the array and returns matching elements in 1D correctly states that boolean indexing flattens and returns matching elements as 1D.
  3. Final Answer:

    Because boolean indexing flattens the array and returns matching elements in 1D -> Option A
  4. Quick Check:

    Boolean indexing flattens multi-dimensional arrays [OK]
Quick Trick: Boolean indexing flattens arrays to 1D output [OK]
Common Mistakes:
  • Thinking numpy doesn't support 2D boolean indexing
  • Believing arrays convert to lists
  • Assuming condition must be 1D

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes