0
0
NumPydata~5 mins

Why boolean masking matters in NumPy

Choose your learning style9 modes available
Introduction

Boolean masking helps you pick out only the data you want from a big set. It makes finding and working with specific parts easy and fast.

You want to find all students who scored above 80 in a test.
You need to select only the days when the temperature was below freezing.
You want to filter out bad data points from a sensor reading.
You want to quickly find all products that are out of stock.
You want to analyze only the sales made in a specific region.
Syntax
NumPy
masked_array = original_array[boolean_condition]
The boolean_condition is an array of True/False values the same size as original_array.
Only elements where the condition is True are kept in masked_array.
Examples
This picks numbers greater than 25 from the array.
NumPy
import numpy as np
arr = np.array([10, 20, 30, 40, 50])
mask = arr > 25
filtered = arr[mask]
print(filtered)
This selects only odd numbers from the array.
NumPy
import numpy as np
arr = np.array([5, 15, 25, 35, 45])
filtered = arr[arr % 2 == 1]
print(filtered)
Sample Program

This program shows how to find temperatures below zero using boolean masking. It prints the original temperatures, the mask of True/False for cold days, and the filtered cold temperatures.

NumPy
import numpy as np

# Create an array of temperatures in Celsius
temps = np.array([22, -5, 15, 0, -10, 30, 5])

# Create a mask for temperatures below zero
cold_days = temps < 0

# Use boolean masking to get only cold days
cold_temps = temps[cold_days]

print("All temperatures:", temps)
print("Cold days mask:", cold_days)
print("Temperatures below zero:", cold_temps)
OutputSuccess
Important Notes

Boolean masks must be the same shape as the array you want to filter.

Boolean masking is very fast and works well with large data sets.

You can combine multiple conditions using & (and) and | (or) with parentheses.

Summary

Boolean masking helps select specific data easily.

It uses True/False arrays to pick elements.

It is useful for filtering and analyzing data quickly.