What if you could count thousands of True answers in just one line of code?
Why Counting with boolean arrays in NumPy? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you have a long list of survey answers marked as True or False, and you want to count how many people answered True.
Doing this by checking each answer one by one on paper or with a simple loop can be tiring and slow.
Manually counting True values means going through each item slowly, risking mistakes like skipping some or counting twice.
It takes a lot of time and effort, especially if the list is very long.
Using boolean arrays with numpy lets you count all True values instantly with a simple command.
This method is fast, accurate, and easy to use, even for very large datasets.
count = 0 for answer in answers: if answer == True: count += 1
count = np.sum(answers)You can quickly find how many items meet a condition, unlocking fast data analysis and decision-making.
Counting how many customers clicked a button on a website (True) versus those who didn't (False) to measure campaign success.
Manual counting is slow and error-prone.
Boolean arrays let you count True values instantly.
This speeds up data analysis and reduces mistakes.
Practice
arr = np.array([True, False, True])What is
arr.sum()?Solution
Step 1: Understand boolean values in NumPy
In NumPy, True is treated as 1 and False as 0 when summed.Step 2: Sum the boolean array
Summing[True, False, True]is 1 + 0 + 1 = 2.Final Answer:
Counts how many True values are in the array -> Option CQuick Check:
True count = 2 [OK]
- Thinking sum counts False values
- Believing sum returns array length
- Expecting an error when summing booleans
arr = np.array([1, 2, 3, 4]) are greater than 2 using boolean arrays?Solution
Step 1: Create boolean array for condition
arr > 2creates a boolean array: [False, False, True, True].Step 2: Sum the boolean array correctly
We must sum the boolean array, so use parentheses:(arr > 2).sum().Final Answer:
(arr > 2).sum() -> Option AQuick Check:
Correct syntax uses parentheses [OK]
- Missing parentheses causing wrong order
- Using sum() on condition without parentheses
- Confusing comparison and sum order
import numpy as np arr = np.array([5, 3, 8, 1, 6]) count = (arr % 2 == 0).sum() print(count)
Solution
Step 1: Create boolean array for even numbers
arr % 2 == 0checks which elements are even: [False, False, True, False, True].Step 2: Sum True values to count evens
Sum is 0 + 0 + 1 + 0 + 1 = 2.Final Answer:
2 -> Option DQuick Check:
Count of even numbers = 2 [OK]
- Counting odd numbers instead
- Forgetting to use parentheses
- Misunderstanding modulo operator
arr are less than 10, but it raises an error. What is the error and how to fix it?import numpy as np arr = np.array([7, 12, 5, 20]) count = arr < 10.sum() print(count)
Solution
Step 1: Identify the error in expression
10.sum()is invalid because 10 is an integer, not an array.Step 2: Correct the syntax to sum boolean array
Use parentheses to sum the boolean array:(arr < 10).sum().Final Answer:
Error: 10.sum() is invalid; fix by using (arr < 10).sum() -> Option BQuick Check:
Parentheses needed before sum() [OK]
- Calling sum() on number instead of boolean array
- Confusing comparison and sum order
- Ignoring error message details
data = np.array([[3, 7, 2], [5, 1, 8], [6, 4, 9]]), how can you count how many elements are greater than 5 across the entire array?Solution
Step 1: Create boolean array for elements > 5
data > 5creates a boolean array marking elements greater than 5.Step 2: Sum True values to count elements
Use(data > 5).sum()to count all True values in the 2D array.Final Answer:
(data > 5).sum() -> Option AQuick Check:
Sum boolean mask counts elements > 5 [OK]
- Using sum() on data directly
- Trying to use count() method on NumPy array
- Using non-existent np.count() function
