Bird
Raised Fist0
NumPydata~10 mins

Boolean indexing in NumPy - Step-by-Step Execution

Choose your learning style10 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.

Practice

(1/5)
1. What does boolean indexing in numpy allow you to do?
easy
A. Create a new array filled with zeros
B. Change the data type of an array
C. Sort the array elements in ascending order
D. Select elements from an array using a True/False mask

Solution

  1. Step 1: Understand boolean indexing concept

    Boolean indexing uses a True/False array to pick elements from another array.
  2. Step 2: Identify the correct operation

    Only Select elements from an array using a True/False mask describes selecting elements based on True/False values.
  3. Final Answer:

    Select elements from an array using a True/False mask -> Option D
  4. Quick Check:

    Boolean indexing = Select elements [OK]
Hint: Boolean mask picks elements where True appears [OK]
Common Mistakes:
  • Confusing boolean indexing with sorting
  • Thinking it changes data types
  • Assuming it creates new arrays with zeros
2. Which of the following is the correct syntax to select elements greater than 5 from a numpy array arr using boolean indexing?
easy
A. arr[arr < 5]
B. arr > 5[arr]
C. arr[arr > 5]
D. arr.select(arr > 5)

Solution

  1. Step 1: Understand boolean indexing syntax

    The correct syntax is to use the array with a boolean mask inside square brackets: arr[condition].
  2. Step 2: Check each option

    arr[arr > 5] uses arr[arr > 5], which is correct. Others have wrong order or invalid methods.
  3. Final Answer:

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

    Correct syntax = arr[condition] [OK]
Hint: Put condition inside brackets after array name [OK]
Common Mistakes:
  • Placing condition before array name
  • Using invalid methods like select()
  • Using wrong comparison operators
3. What is the output of the following code?
import numpy as np
arr = np.array([2, 7, 4, 9, 1])
result = arr[arr % 2 == 1]
medium
A. [7 9 1]
B. [2 4]
C. [7 4 9]
D. [2 7 4 9 1]

Solution

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

    This condition selects elements where the remainder when divided by 2 is 1, i.e., odd numbers.
  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 A
  4. Quick Check:

    Odd elements = [7 9 1] [OK]
Hint: Filter with modulo to get odd numbers [OK]
Common Mistakes:
  • Selecting even numbers instead
  • Confusing modulo operator
  • Returning full array instead of filtered
4. The following code has a problem. What is the problem?
import numpy as np
arr = np.array([1, 2, 3, 4])
mask = arr > 2
print(arr[mask == True])
medium
A. mask == True is unnecessary and redundant
B. arr should be converted to list first
C. Comparison operator > is invalid for numpy arrays
D. Square brackets cannot be used for indexing

Solution

  1. Step 1: Analyze the boolean mask creation

    mask = arr > 2 creates a boolean array correctly.
  2. Step 2: Check the indexing expression

    Using mask == True is redundant since mask is already boolean. It's unnecessary and bad practice.
  3. Step 3: Correct usage

    Use arr[mask] directly without comparing mask to True.
  4. Final Answer:

    mask == True is unnecessary and redundant -> Option A
  5. Quick Check:

    Use boolean mask directly [OK]
Hint: Use mask directly, no need for '== True' [OK]
Common Mistakes:
  • Adding '== True' to boolean mask
  • Trying to convert numpy array to list for indexing
  • Misunderstanding comparison operators
5. Given a numpy array data = np.array([10, 15, 20, 25, 30]), how can you select elements greater than 12 and less than 28 using boolean indexing?
hard
A. data[data > 12 and data < 28]
B. data[(data > 12) & (data < 28)]
C. data[data > 12 | data < 28]
D. data[(data > 12) or (data < 28)]

Solution

  1. Step 1: Understand combining conditions with & and parentheses

    In numpy, use & for element-wise AND and wrap each condition in parentheses.
  2. Step 2: Check each option

    data[(data > 12) & (data < 28)] correctly uses & (element-wise AND) with parentheses around each condition. Using Python keywords and/or cause errors because they don't work element-wise on arrays. Using | (bitwise OR) selects elements greater than 12 or less than 28, which is wrong here.
  3. Final Answer:

    data[(data > 12) & (data < 28)] -> Option B
  4. Quick Check:

    Use & with parentheses for AND conditions [OK]
Hint: Use & with parentheses for multiple conditions [OK]
Common Mistakes:
  • Using 'and' or 'or' instead of & or |
  • Not using parentheses around conditions
  • Using | (OR) instead of & (AND) for filtering
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard