0
0
NumPydata~10 mins

Counting with boolean arrays in NumPy - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Counting with boolean arrays
Create boolean array
Apply condition to get boolean array
Count True values using sum()
Output count
We create a boolean array by applying a condition, then count how many True values it has using sum.
Execution Sample
NumPy
import numpy as np
arr = np.array([1, 3, 5, 2, 4])
bool_arr = arr > 3
count = bool_arr.sum()
print(count)
This code counts how many numbers in the array are greater than 3.
Execution Table
StepActionVariableValueExplanation
1Create arrayarr[1 3 5 2 4]Original numeric array
2Apply condition arr > 3bool_arr[False False True False True]Boolean array where True means element > 3
3Count True valuescount2Sum counts True as 1, so total is 2
4Print countoutput2Output shows how many elements are > 3
💡 All steps done, count of True values calculated and printed
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
arrNone[1 3 5 2 4][1 3 5 2 4][1 3 5 2 4][1 3 5 2 4]
bool_arrNoneNone[False False True False True][False False True False True][False False True False True]
countNoneNoneNone22
Key Moments - 3 Insights
Why does summing a boolean array count True values?
In numpy, True is treated as 1 and False as 0 when summed, so sum counts how many True values exist (see execution_table step 3).
What if the condition returns no True values?
Then the boolean array has all False, sum returns 0, meaning no elements met the condition (not shown here but same logic applies).
Can we count False values the same way?
Yes, by summing the negation (~bool_arr) or using (bool_arr == False), sum counts False values.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of bool_arr after step 2?
A[False True False True False]
B[False False True False True]
C[True True False True False]
D[True False True False True]
💡 Hint
Check the 'Value' column for bool_arr at step 2 in execution_table.
At which step is the count of True values calculated?
AStep 3
BStep 1
CStep 2
DStep 4
💡 Hint
Look for the step where 'count' variable gets a numeric value in execution_table.
If the array was arr = np.array([0, 1, 2]), what would count be after applying arr > 3?
A1
B3
C0
D2
💡 Hint
Refer to how sum counts True values; none of these numbers are > 3, so sum is zero.
Concept Snapshot
Counting with boolean arrays:
- Apply a condition to get a boolean array (True/False)
- True counts as 1, False as 0
- Use sum() on boolean array to count True values
- Useful for quick counts of elements meeting a condition
Full Transcript
We start by creating a numpy array of numbers. Then we apply a condition (like greater than 3) to get a boolean array where each element is True if it meets the condition, False otherwise. Next, we sum the boolean array. Since True counts as 1 and False as 0, the sum gives the count of elements meeting the condition. Finally, we print this count. This method is simple and fast for counting with conditions in numpy.