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:
Step 1: Understand boolean indexing behavior on multi-dimensional arrays
Boolean indexing selects elements matching condition and returns them flattened.
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.
Final Answer:
Because boolean indexing flattens the array and returns matching elements in 1D -> Option A