We use np.count_nonzero() to quickly count how many values in data are not zero. It helps us find important information fast.
np.count_nonzero() for counting in NumPy
Start learning this pattern below
Jump into concepts and practice - no test required
np.count_nonzero(a, axis=None, keepdims=False)
a is the input array where you want to count non-zero values.
axis lets you count along rows, columns, or the whole array.
np.count_nonzero([1, 0, 2, 0, 3])
np.count_nonzero([[0, 1], [2, 0]], axis=0)
np.count_nonzero([[0, 1], [2, 0]], axis=1)
This program counts how many sales are non-zero in total, per day, and per product using np.count_nonzero().
import numpy as np # Create a 2D array representing sales for 3 days and 4 products sales = np.array([[0, 5, 0, 3], [2, 0, 0, 0], [0, 0, 1, 4]]) # Count total non-zero sales total_nonzero = np.count_nonzero(sales) # Count non-zero sales per day (row) nonzero_per_day = np.count_nonzero(sales, axis=1) # Count non-zero sales per product (column) nonzero_per_product = np.count_nonzero(sales, axis=0) print(f"Total non-zero sales: {total_nonzero}") print(f"Non-zero sales per day: {nonzero_per_day}") print(f"Non-zero sales per product: {nonzero_per_product}")
Zero means 'no data' or 'no event' in many cases, so counting non-zero helps find actual occurrences.
You can use axis to count along rows or columns for more detailed analysis.
np.count_nonzero() counts how many values are not zero in data.
It works on arrays of any shape and can count overall or by rows/columns.
This function is useful to quickly find how many events or values exist in your data.
Practice
What does the np.count_nonzero() function do in NumPy?
Solution
Step 1: Understand the function purpose
np.count_nonzero()counts elements that are not zero in the array.Step 2: Compare with other options
Other options describe different functions like sum, max, or shape, which are not whatnp.count_nonzero()does.Final Answer:
Counts how many elements in an array are not zero -> Option DQuick Check:
Counting non-zero elements = Counts how many elements are not zero [OK]
- Confusing count_nonzero with sum or max functions
- Thinking it returns the array shape
- Assuming it counts zero elements
Which of the following is the correct syntax to count non-zero elements in a NumPy array arr?
arr = np.array([1, 0, 3, 0, 5])Solution
Step 1: Identify correct function usage
The functionnp.count_nonzero()is called with the array as argument:np.count_nonzero(arr).Step 2: Check other options for errors
np.count_nonzero = arr tries to assign instead of call; arr.count_nonzero() uses method not available on array; np.count(arr != 0) uses a non-existent functionnp.count().Final Answer:
np.count_nonzero(arr) -> Option BQuick Check:
Correct syntax is np.count_nonzero(array) [OK]
- Using assignment instead of function call
- Calling count_nonzero as a method on array
- Using non-existent np.count function
What is the output of the following code?
import numpy as np
arr = np.array([[0, 1, 2], [3, 0, 0], [4, 5, 6]])
count = np.count_nonzero(arr, axis=0)
print(count)Solution
Step 1: Understand axis=0 counting
Counting non-zero elements along columns (axis=0) means counting down each column.Step 2: Count non-zero per column
Column 1: values [0,3,4] -> non-zero count = 2 (3 and 4)
Column 2: values [1,0,5] -> non-zero count = 2 (1 and 5)
Column 3: values [2,0,6] -> non-zero count = 2 (2 and 6)Final Answer:
[3 2 3] -> Option AQuick Check:
Count non-zero per column = [3 2 3] [OK]
- Counting zeros instead of non-zero
- Confusing axis=0 with axis=1
- Miscounting elements per column
Find the error in this code snippet and choose the correct fix:
import numpy as np
arr = np.array([1, 0, 2, 0, 3])
count = np.count_nonzero(arr, axis=1)
print(count)Solution
Step 1: Identify array dimension
Arrayarris 1D, so axis=1 is invalid (no second axis).Step 2: Correct function call
Remove axis argument to count all non-zero elements:np.count_nonzero(arr).Final Answer:
Remove axis=1 because arr is 1D, use np.count_nonzero(arr) instead -> Option AQuick Check:
1D arrays have no axis=1, so omit axis [OK]
- Using axis=1 on 1D arrays causes errors
- Trying to call count_nonzero as array method
- Assuming axis=0 fixes all axis errors
You have a 2D NumPy array representing attendance (1 for present, 0 for absent) of 4 students over 5 days:
attendance = np.array([
[1, 0, 1, 1, 0],
[0, 0, 1, 0, 0],
[1, 1, 1, 1, 1],
[0, 0, 0, 0, 0]
])Which code correctly counts how many days each student was present?
Solution
Step 1: Understand data layout
Rows represent students, columns represent days. Counting days present per student means counting non-zero per row (axis=1).Step 2: Choose correct axis
Usenp.count_nonzero(attendance, axis=1)to count non-zero values per student (row).Final Answer:
np.count_nonzero(attendance, axis=1) -> Option CQuick Check:
Count per row (student) = axis=1 [OK]
- Using axis=0 counts per day, not per student
- Using np.sum instead of count_nonzero (works but different function)
- Counting total non-zero without axis
