0
0
NumPydata~10 mins

Boolean indexing in NumPy - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Boolean indexing
Start with array
Create boolean mask
Apply mask to array
Get filtered array
Use filtered array for analysis or output
Boolean indexing uses a mask of True/False values to select elements from an array.
Execution Sample
NumPy
import numpy as np
arr = np.array([10, 15, 20, 25, 30])
mask = arr > 20
filtered = arr[mask]
print(filtered)
This code selects elements from arr that are greater than 20 using boolean indexing.
Execution Table
StepActionArray 'arr'Mask 'mask'Filtered array 'filtered'
1Create array[10, 15, 20, 25, 30]N/AN/A
2Create mask arr > 20[10, 15, 20, 25, 30][False, False, False, True, True]N/A
3Apply mask to arr[10, 15, 20, 25, 30][False, False, False, True, True][25, 30]
4Print filtered[10, 15, 20, 25, 30][False, False, False, True, True][25, 30]
💡 Finished filtering; mask selects elements > 20, resulting in [25, 30]
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
arrN/A[10, 15, 20, 25, 30][10, 15, 20, 25, 30][10, 15, 20, 25, 30][10, 15, 20, 25, 30]
maskN/AN/A[False, False, False, True, True][False, False, False, True, True][False, False, False, True, True]
filteredN/AN/AN/A[25, 30][25, 30]
Key Moments - 3 Insights
Why does the mask have True only for some elements?
The mask is created by comparing each element to 20 (arr > 20). Only elements greater than 20 get True, as shown in step 2 of the execution_table.
What happens if we use the mask directly without applying it to arr?
The mask itself is just True/False values, not the filtered data. You must apply it to arr (arr[mask]) to get the selected elements, as in step 3.
Can the filtered array have fewer elements than the original?
Yes, because the mask selects only elements where the condition is True, so filtered array is smaller or equal in size, shown in step 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2, what is the value of mask?
A[False, False, False, True, True]
B[True, True, False, False, False]
C[False, True, True, False, True]
D[True, False, True, True, False]
💡 Hint
Check the 'Mask mask' column at step 2 in the execution_table.
At which step does the filtered array get its values?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Filtered array filtered' column in the execution_table to see when it changes from N/A.
If we change the condition to arr >= 25, how would the mask change at step 2?
A[False, False, False, True, True]
B[False, False, False, False, True]
C[False, False, False, True, False]
D[True, True, True, True, True]
💡 Hint
Compare arr values to 25 and mark True where arr >= 25, then check mask pattern.
Concept Snapshot
Boolean indexing in numpy:
- Create a boolean mask by comparing array elements.
- Use mask to select elements: filtered = arr[mask]
- Mask is same shape as array, with True/False.
- Result is array of elements where mask is True.
- Useful for filtering data easily.
Full Transcript
Boolean indexing lets you pick elements from a numpy array using True/False values. First, you make a mask by checking a condition on each element. For example, arr > 20 creates a mask with True where elements are greater than 20. Then you use this mask to get only those elements from the array by writing arr[mask]. The filtered array contains only the selected elements. This method is simple and powerful for filtering data in numpy.