🎭 Boolean Indexing
Boolean indexing filters data using True/False conditions. Create logical masks to find values that meet specific criteria.
import numpy as np
# Boolean indexing overview
scores = np.array([85, 92, 78, 96, 89, 74, 88, 93])
names = np.array(['Alice', 'Bob', 'Carol', 'David',
'Eve', 'Frank', 'Grace', 'Henry'])
# Create boolean mask
high_scores = scores > 90
print(f"High scores mask: {high_scores}")
print(f"High performers: {names[high_scores]}")
print(f"Their scores: {scores[high_scores]}")
🔍 Creating Boolean Masks
Boolean masks are arrays of True/False values that act as filters.
Basic Comparisons
import numpy as np
temperatures = np.array([22, 25, 18, 30, 27, 15, 32, 24])
# Create different masks
hot_days = temperatures > 25
cold_days = temperatures < 20
perfect_days = temperatures == 25
print(f"Temperatures: {temperatures}")
print(f"Hot days (>25°C): {hot_days}")
print(f"Number of hot days: {hot_days.sum()}")
String Comparisons
import numpy as np
fruits = np.array(['apple', 'banana', 'cherry', 'date'])
colors = np.array(['red', 'yellow', 'red', 'brown'])
# String conditions
red_fruits = colors == 'red'
has_e = np.char.find(fruits, 'e') >= 0
print(f"Red fruits: {fruits[red_fruits]}")
print(f"Contains 'e': {fruits[has_e]}")
🔗 Combining Conditions
Use logical operators to combine multiple conditions.
AND Conditions
import numpy as np
scores = np.array([85, 92, 78, 96, 89, 74, 88, 93])
ages = np.array([20, 22, 19, 21, 23, 20, 22, 21])
names = np.array(['Alice', 'Bob', 'Carol', 'David',
'Eve', 'Frank', 'Grace', 'Henry'])
# Multiple conditions with AND
high_achievers = (scores > 85) & (ages < 22)
print(f"High achievers under 22: {names[high_achievers]}")
# Range conditions
good_range = (scores >= 80) & (scores <= 90)
print(f"Scores 80-90: {names[good_range]}")
OR Conditions
import numpy as np
scores = np.array([85, 92, 78, 96, 89, 74, 88, 93])
names = np.array(['Alice', 'Bob', 'Carol', 'David',
'Eve', 'Frank', 'Grace', 'Henry'])
# Either condition true
extreme_scores = (scores > 95) | (scores < 75)
print(f"Very high or low scores: {names[extreme_scores]}")
# Multiple OR conditions
need_help = (scores < 80) | (scores == 78)
print(f"Students needing help: {names[need_help]}")
NOT Conditions
import numpy as np
scores = np.array([85, 92, 78, 96, 89, 74, 88, 93])
names = np.array(['Alice', 'Bob', 'Carol', 'David',
'Eve', 'Frank', 'Grace', 'Henry'])
# NOT operator
not_excellent = ~(scores > 90)
print(f"Not excellent (≤90): {names[not_excellent]}")
# Combining NOT with other conditions
decent_not_great = (scores >= 80) & ~(scores > 90)
print(f"Decent but not great: {names[decent_not_great]}")
🎯 2D Boolean Indexing
Apply boolean conditions to multi-dimensional arrays.
Row-wise Filtering
import numpy as np
# Student data: [Math, Science, English]
grades = np.array([[85, 92, 78], # Alice
[79, 85, 91], # Bob
[94, 89, 96], # Carol
[72, 78, 74]]) # David
students = np.array(['Alice', 'Bob', 'Carol', 'David'])
# Filter based on Math scores
good_math = grades[:, 0] > 80
print(f"Good at Math: {students[good_math]}")
# Filter based on average
averages = grades.mean(axis=1)
high_average = averages > 85
print(f"High average: {students[high_average]}")
Element-wise Filtering
import numpy as np
data = np.array([[10, 25, 30],
[15, 5, 35],
[20, 45, 10]])
# Find all values > 20
high_values = data > 20
print(f"Original: \n{data}")
print(f"Values > 20: {data[high_values]}")
# Replace values conditionally
data_copy = data.copy()
data_copy[data_copy < 15] = 0 # Set low values to 0
print(f"After replacing <15 with 0: \n{data_copy}")
🧮 Using np.where()
np.where()
provides conditional selection with custom values.
Basic np.where()
import numpy as np
scores = np.array([85, 92, 78, 96, 89, 74, 88, 93])
# Convert to pass/fail
pass_fail = np.where(scores >= 80, 'Pass', 'Fail')
print(f"Scores: {scores}")
print(f"Result: {pass_fail}")
# Convert to letter grades
grades = np.where(scores >= 90, 'A',
np.where(scores >= 80, 'B', 'C'))
print(f"Letter grades: {grades}")
Advanced np.where()
import numpy as np
temperatures = np.array([22, 35, 18, 40, 27, 15, 32])
# Multiple conditions
weather = np.where(temperatures > 30, 'Hot',
np.where(temperatures < 20, 'Cold', 'Nice'))
print(f"Temperatures: {temperatures}")
print(f"Weather: {weather}")
# Mathematical operations
adjusted = np.where(temperatures > 30,
temperatures - 5, # Cool down hot days
temperatures + 2) # Warm up other days
print(f"Adjusted temps: {adjusted}")
🧠 Practice Exercise
import numpy as np
# Sales data: [Q1, Q2, Q3, Q4]
sales = np.array([[120, 135, 145, 160], # Store A
[98, 112, 125, 140], # Store B
[156, 167, 175, 185], # Store C
[89, 95, 105, 125]]) # Store D
stores = np.array(['Store A', 'Store B', 'Store C', 'Store D'])
# Find stores with strong Q4
strong_q4 = sales[:, 3] > 150
print(f"Strong Q4 (>150): {stores[strong_q4]}")
# Find consistent performers (all quarters > 100)
consistent = (sales > 100).all(axis=1)
print(f"Consistent performers: {stores[consistent]}")
# Find growth pattern (Q4 > Q1)
growing = sales[:, 3] > sales[:, 0]
growth_amount = sales[:, 3] - sales[:, 0]
print(f"Growing stores: {stores[growing]}")
print(f"Growth amounts: {growth_amount[growing]}")
🎯 Key Takeaways
🚀 What's Next?
You've mastered array indexing! Next, learn array manipulation techniques.
Continue to: Array Manipulation
Was this helpful?
Track Your Learning Progress
Sign in to bookmark tutorials and keep track of your learning journey.
Your progress is saved automatically as you read.