๐ง โ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