0
0
NumPydata~5 mins

Counting with boolean arrays in NumPy - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does a boolean array represent in numpy?
A boolean array in numpy contains True or False values, often used to mark conditions or filters on data.
Click to reveal answer
beginner
How can you count the number of True values in a numpy boolean array?
You can use the np.sum() function on the boolean array because True is treated as 1 and False as 0.
Click to reveal answer
intermediate
Why does np.sum() work for counting True values in a boolean array?
Because in numpy, True is equivalent to 1 and False is equivalent to 0, so summing counts how many True values exist.
Click to reveal answer
intermediate
What is the difference between np.count_nonzero() and np.sum() when used on boolean arrays?
np.count_nonzero() counts all non-zero (True) elements, similar to np.sum() on boolean arrays. Both give the count of True values.
Click to reveal answer
beginner
How can boolean arrays help in real-life data analysis?
Boolean arrays help filter data, count conditions met, and quickly summarize information like how many entries meet a rule.
Click to reveal answer
What does np.sum() return when applied to a boolean numpy array?
AThe count of True values
BThe count of False values
CThe sum of True and False as strings
DAn error because sum can't be used on booleans
Which numpy function can also count True values in a boolean array besides np.sum()?
Anp.unique()
Bnp.count_nonzero()
Cnp.min()
Dnp.mean()
In numpy, what numeric value does False correspond to when used in calculations?
A-1
B1
C0
DNone
If you have a boolean array [True, False, True], what does np.sum() return?
A3
B0
C1
D2
Why are boolean arrays useful in data science?
AThey help filter and count data based on conditions
BThey store large text data efficiently
CThey replace all numeric data
DThey slow down calculations
Explain how you can count how many values in a numpy array meet a certain condition using boolean arrays.
Think about how True and False behave as numbers in numpy.
You got /3 concepts.
    Describe two numpy functions that can count True values in a boolean array and how they work.
    Both functions treat True as a positive count.
    You got /4 concepts.