Bird
Raised Fist0
NumPydata~10 mins

Boolean indexing for filtering in NumPy - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Concept Flow - Boolean indexing for filtering
Start with array
↓
Create boolean condition array
↓
Use boolean array to select elements
↓
Return filtered array
We start with an array, create a boolean array by applying a condition, then use it to pick elements that match the condition.
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 filters elements of arr that are greater than 20 using boolean indexing.
Execution Table
StepActionExpressionResult
1Create arrayarr = np.array([10, 15, 20, 25, 30])[10 15 20 25 30]
2Apply condition > 20mask = arr > 20[False False False True True]
3Filter array using maskfiltered = arr[mask][25 30]
4Print filtered arrayprint(filtered)[25 30]
💡 Filtering done, only elements > 20 selected
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
arrundefined[10 15 20 25 30][10 15 20 25 30][10 15 20 25 30][10 15 20 25 30]
maskundefinedundefined[False False False True True][False False False True True][False False False True True]
filteredundefinedundefinedundefined[25 30][25 30]
Key Moments - 3 Insights
Why does the mask array contain True and False values?
The mask is created by checking each element of arr against the condition arr > 20. Elements greater than 20 get True, others get False, as shown in step 2 of the execution_table.
How does arr[mask] select elements?
arr[mask] picks elements where mask is True. It ignores elements where mask is False. This is shown in step 3 where filtered contains only [25 30].
Can the mask array be used directly for filtering without creating a new variable?
Yes, you can use arr[arr > 20] directly. Creating mask separately helps understand the process, but both ways produce the same filtered result.
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 True False False]
C[False True False True False]
D[True False True False True]
💡 Hint
Check the 'Result' column for step 2 in execution_table.
At which step does the filtered array get its final values?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'filtered' variable in variable_tracker and execution_table step 3.
If the condition changes to arr >= 25, what would be the filtered array?
A[25 30]
B[20 25 30]
C[30]
D[15 20 25 30]
💡 Hint
Consider which elements in arr are greater than or equal to 25.
Concept Snapshot
Boolean indexing filters arrays by creating a boolean mask.
Syntax: filtered = arr[condition]
Condition returns True/False for each element.
Only elements with True are selected.
Useful for quick filtering without loops.
Full Transcript
Boolean indexing in numpy lets you filter arrays easily. You start with an array, then create a boolean mask by applying a condition to each element. This mask has True where the condition is met and False elsewhere. Using this mask to index the array returns only the elements where the mask is True. For example, arr > 20 creates a mask that is True for elements greater than 20. Then arr[mask] returns those elements. This method is simple and fast for filtering data.

Practice

(1/5)
1. What does boolean indexing in numpy allow you to do?
easy
A. Select elements from an array based on True/False conditions
B. Sort an array in ascending order
C. Change the data type of an array
D. Calculate the sum of all elements in an array

Solution

  1. Step 1: Understand boolean indexing concept

    Boolean indexing uses a True/False array to pick elements from another array.
  2. Step 2: Compare with other options

    Sorting, changing data type, and summing are different numpy operations, not boolean indexing.
  3. Final Answer:

    Select elements from an array based on True/False conditions -> Option A
  4. Quick Check:

    Boolean indexing = filtering by True/False [OK]
Hint: Boolean indexing picks elements where condition is True [OK]
Common Mistakes:
  • Confusing boolean indexing with sorting
  • Thinking it changes data types
  • Assuming it calculates sums
2. Which of the following is the correct syntax to filter array arr for values greater than 5 using boolean indexing?
easy
A. arr > 5[arr]
B. arr[arr > 5]
C. arr.filter(arr > 5)
D. arr[arr < 5]

Solution

  1. Step 1: Identify correct boolean indexing syntax

    In numpy, filtering uses arr[condition] where condition is a boolean array.
  2. Step 2: Check each option

    arr[arr > 5] uses correct syntax. arr > 5[arr] is invalid syntax. arr.filter(arr > 5) is not a numpy method. arr[arr < 5] filters for less than 5, not greater.
  3. Final Answer:

    arr[arr > 5] -> Option B
  4. Quick Check:

    Correct syntax is arr[condition] [OK]
Hint: Use arr[condition] to filter arrays in numpy [OK]
Common Mistakes:
  • Placing condition outside brackets
  • Using non-existent filter method
  • Mixing up greater than and less than
3. What is the output of the following code?
import numpy as np
arr = np.array([2, 7, 4, 9, 1])
filtered = arr[arr % 2 == 1]
medium
A. [7 4 9]
B. [2 4]
C. [7 9 1]
D. [2 7 4 9 1]

Solution

  1. Step 1: Understand the condition arr % 2 == 1

    This condition selects odd numbers because odd numbers have remainder 1 when divided by 2.
  2. Step 2: Apply condition to array elements

    Elements 7, 9, and 1 are odd, so they are selected.
  3. Final Answer:

    [7 9 1] -> Option C
  4. Quick Check:

    Filter odd numbers = [7 9 1] [OK]
Hint: Use modulo (%) to filter odd/even numbers [OK]
Common Mistakes:
  • Selecting even numbers instead of odd
  • Including all elements without filtering
  • Misunderstanding modulo operator
4. The following code throws an error. What is the mistake?
import numpy as np
arr = np.array([10, 15, 20, 25])
filtered = arr[arr > 15 and arr < 25]
medium
A. Using 'and' instead of '&' for element-wise condition
B. Missing parentheses around conditions
C. Using 'or' instead of 'and'
D. Array is not defined properly

Solution

  1. Step 1: Identify boolean operator error

    In numpy, element-wise logical operations require '&' instead of Python's 'and'.
  2. Step 2: Understand why 'and' causes error

    'and' expects single boolean, but arr > 15 and arr < 25 returns arrays, causing TypeError.
  3. Final Answer:

    Using 'and' instead of '&' for element-wise condition -> Option A
  4. Quick Check:

    Use '&' for element-wise logical AND [OK]
Hint: Use & with parentheses for multiple conditions [OK]
Common Mistakes:
  • Using 'and' instead of '&' in numpy conditions
  • Forgetting parentheses around each condition
  • Assuming 'or' works like '|'
5. Given a numpy array data = np.array([3, 6, 9, 12, 15, 18]), how would you filter values that are divisible by 3 but not by 6 using boolean indexing?
hard
A. data[(data % 3 == 0) & (data % 6 == 0)]
B. data[(data % 3 == 0) | (data % 6 != 0)]
C. data[(data % 3 != 0) & (data % 6 == 0)]
D. data[(data % 3 == 0) & (data % 6 != 0)]

Solution

  1. Step 1: Define conditions for filtering

    We want numbers divisible by 3 (data % 3 == 0) but not divisible by 6 (data % 6 != 0).
  2. Step 2: Combine conditions with element-wise AND

    Use '&' to combine both conditions inside parentheses for correct boolean indexing.
  3. Step 3: Apply combined condition to data array

    data[(data % 3 == 0) & (data % 6 != 0)] correctly applies both conditions with '&'. Others use wrong operators or conditions.
  4. Final Answer:

    data[(data % 3 == 0) & (data % 6 != 0)] -> Option D
  5. Quick Check:

    Use & and parentheses for combined conditions [OK]
Hint: Combine conditions with & and parentheses for filtering [OK]
Common Mistakes:
  • Using | instead of & for AND condition
  • Mixing up divisibility conditions
  • Forgetting parentheses around each condition