Course contentsShow
← Machine Learning and Deep Learning
Lesson 153 of 3,5382. Machine Learning Mindset and Core ConceptsFree lesson

Boolean Indexing and Masking

Using conditional expressions to filter data, select subsets, and handle missing values in ML workflows.

Boolean Indexing and Masking

What you'll learn: How to use True/False conditions to filter and select exactly the data you need for machine learning tasks.

The Core Idea

Boolean indexing lets you ask questions about your data and automatically select only the rows (or elements) where the answer is "yes" (True). Think of it like a filter in a spreadsheet that shows only rows meeting certain criteria—but much more powerful and code-based.

Instead of manually specifying positions like "give me rows 3, 7, and 12," you describe what you want: "give me all samples where the temperature exceeds 30" or "show me records with missing values."

How It Works

When you apply a conditional expression to a NumPy array, you get back a boolean mask—an array of True/False values with the same shape. This mask then acts as a filter:

import numpy as np

# Example: filtering training data
temperatures = np.array([28, 35, 22, 31, 19])

# Create a boolean mask
mask = temperatures > 30
# Result: [False, True, False, True, False]

# Use the mask to filter
hot_days = temperatures[mask]
# Result: [35, 31]

ML Applications

This technique is essential for:

  • Filtering outliers: Remove extreme values that might hurt model training
  • Handling missing values: Select only complete records or identify gaps
  • Creating subsets: Split data by category (e.g., all "positive" class examples)
  • Data cleaning: Find and fix problematic entries before training

Key Takeaway: Boolean indexing transforms conditional questions into instant data filters—a fundamental operation for preparing clean, relevant datasets in every ML workflow.