NumPy supports a powerful technique calledBoolean indexing(also called Boolean masking) to filter arrays based on a condition. When you write bmi > 23, NumPy performs an element-wise comparison and produces a Boolean array of the same shape, containing True where the condition holds and False otherwise. Using that Boolean array inside square brackets, as in bmi[bmi > 23], tells NumPy to return a new 1D array containing only the elements whose mask value is True. This approach is heavily emphasized in scientific computing curricula because it expresses selection logic without explicit loops and runs efficiently in optimized compiled code.
Option B looks close but is not standard NumPy usage. The function commonly used is np.where(condition) or np.where(condition, x, y). While np.where(bmi > 23) can return indices, bmi.where(...) is not a NumPy array method; it is more associated with pandas objects. Options A and C are not valid NumPy APIs for filtering.
Boolean indexing is central in data analysis tasks such as removing invalid measurements, selecting a population subgroup, applying thresholds, and building feature subsets. It composes cleanly with vectorized computation, for example bmi[bmi > 23].mean(), enabling concise and high-performance numerical workflows.
Contribute your Thoughts:
Chosen Answer:
This is a voting comment (?). You can switch to a simple comment. It is better to Upvote an existing comment if you don't have anything to add.
Submit