π§ βBoolean indexing in NumPy is like having a magical magnifying glass β you only see the data you care about.β
Welcome to Chapter 8 of your NumPy learning series!
Youβve already mastered creating, reshaping, and manipulating arrays. Now, letβs move into one of the most powerful features of NumPy β Boolean indexing and masking.
This concept is a game-changer for:
-
Filtering data
-
Making condition-based selections
-
Applying vectorized logic without loops
Whether you’re cleaning data, extracting rows that meet a condition, or highlighting specific values β Boolean indexing helps you do it quickly, efficiently, and cleanly.
π What You’ll Learn
-
How to create Boolean masks using conditions
-
Select elements that meet one or more criteria
-
Combine multiple conditions using logical operators
-
Real-life examples: filtering, cleaning, and subsetting data
Letβs begin!
β 1. What is Boolean Indexing?
Boolean indexing means selecting data based on whether a condition is True or False.
Letβs start simple:
import numpy as np arr = np.array([10, 20, 30, 40, 50]) mask = arr > 30 print(mask)
Output:
Now apply the mask:
π― This is Boolean indexing in action: filtering without loops.
π§ 2. Creating Boolean Masks
You can use any comparison operator:
Operator | Description |
---|---|
> |
Greater than |
< |
Less than |
== |
Equal to |
!= |
Not equal |
>= |
Greater or equal |
<= |
Less or equal |
π Examples
π§ 3. Selecting Elements That Meet Criteria
Use a Boolean mask directly inside the arrayβs brackets:
This can be used in real-life for:
-
Selecting rows with high sales
-
Removing missing data
-
Filtering scores above average
π 4. Combining Multiple Conditions
Just like in logic, you can combine multiple conditions using:
Operator | Description |
---|---|
& |
AND |
` | ` |
~ |
NOT |
Wrap conditions in parentheses to avoid precedence errors!
πΈ Example: AND
arr = np.array([10, 15, 20, 25, 30]) # Select values between 15 and 30 filtered = arr[(arr >= 15) & (arr <= 30)] print(filtered) # [15 20 25 30]
πΈ Example: OR
πΈ Example: NOT
π§Ύ 5. Boolean Indexing with 2D Arrays
Letβs take a matrix:
π 6. Updating Elements Based on Conditions
You can also modify values using Boolean masks:
Useful for:
-
Replacing outliers
-
Censoring negative numbers
-
Standardizing data ranges
π‘ 7. Where to Use Boolean Indexing in Real Life?
Application | Use Case |
---|---|
Data Cleaning | Remove NaNs or negative values |
Image Processing | Mask pixels below threshold |
Finance | Select trades > βΉ10,000 |
Machine Learning | Extract samples with target class |
Health Data | Patients with high BP or low sugar |
π 8. Bonus: Using np.where()
Sometimes you donβt just want to filter β you want to choose between two values based on a condition.
Syntax:
Example:
This is especially useful in label encoding, binarizing data, or applying simple transformations.
π§ͺ Real-World Use Case: Filter Students Above Average
You can also extract their indices:
β οΈ Common Mistakes to Avoid
Mistake | Correction |
---|---|
Not using parentheses with & or ` |
` |
Confusing and / or with & / ` |
` |
Expecting shape preservation | Boolean indexing flattens the result |
Modifying original array without copy | Use .copy() if you need the original unchanged |
π Summary Table: Boolean Indexing Essentials
Concept | Example | Result |
---|---|---|
Create mask | arr > 20 |
[False, False, True, ...] |
Apply mask | arr[arr > 20] |
Values > 20 |
AND | (a > 10) & (a < 50) |
Values between 10β50 |
OR | `(a == 0) | (a == 1)` |
NOT | ~(a < 5) |
Values β₯ 5 |
Replace | arr[arr < 0] = 0 |
Set negatives to zero |
np.where() |
np.where(a > 0, 1, 0) |
Replace values by condition |
π Wrapping Up Chapter 8
And thatβs a wrap! π
Youβve just unlocked one of the most powerful tools in NumPy: Boolean indexing and masking.
This feature allows you to:
-
Filter smartly
-
Replace conditionally
-
Combine logic in a clean, vectorized way
Youβll use Boolean indexing in every serious NumPy project, whether you’re analyzing data, building AI, or visualizing stats.
π Coming Next in Chapter 9: Sorting and Searching in NumPy
Weβll explore:
-
Sorting arrays with
np.sort()
,argsort()
-
Finding values with
searchsorted()
,nonzero()
-
Real-world examples with data processing