Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Counting Non-Zero Elements with np.count_nonzero()
📖 Scenario: Imagine you work in a store that tracks daily sales of different products. Some days, some products sell zero items. You want to find out how many products sold at least one item each day.
🎯 Goal: You will create a numpy array representing daily sales, then use np.count_nonzero() to count how many products sold more than zero items each day.
📋 What You'll Learn
Create a numpy array called daily_sales with the exact values given.
Create a variable called day_index to select a specific day.
Use np.count_nonzero() to count non-zero sales for the selected day.
Print the count of products sold on that day.
💡 Why This Matters
🌍 Real World
Stores and businesses often track sales data daily. Counting how many products sold helps understand customer demand and stock management.
💼 Career
Data analysts and scientists use numpy functions like <code>np.count_nonzero()</code> to quickly analyze datasets and extract useful insights.
Progress0 / 4 steps
1
Create the daily sales data
Create a numpy array called daily_sales with these exact values: [[3, 0, 5, 0], [0, 2, 0, 1], [4, 0, 0, 0]]. This array represents sales of 4 products over 3 days.
NumPy
Hint
Use np.array() and type the exact nested list of sales numbers.
2
Select the day to analyze
Create a variable called day_index and set it to 1 to select the second day (indexing starts at 0).
NumPy
Hint
Remember, Python counts from zero, so the second day is index 1.
3
Count products sold on the selected day
Use np.count_nonzero() on daily_sales[day_index] to count how many products sold more than zero items on that day. Store the result in a variable called products_sold.
NumPy
Hint
Pass the selected day's sales array to np.count_nonzero() to count non-zero values.
4
Print the count of products sold
Print the value of products_sold to show how many products sold on the selected day.
NumPy
Hint
Use print(products_sold) to display the count.
Practice
(1/5)
1.
What does the np.count_nonzero() function do in NumPy?
easy
A. Calculates the sum of all elements in an array
B. Returns the shape of the array
C. Finds the maximum value in an array
D. Counts how many elements in an array are not zero
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 what np.count_nonzero() does.
Final Answer:
Counts how many elements in an array are not zero -> Option D
Quick Check:
Counting non-zero elements = Counts how many elements are not zero [OK]
Hint: Remember: count_nonzero counts non-zero values only [OK]
Common Mistakes:
Confusing count_nonzero with sum or max functions
Thinking it returns the array shape
Assuming it counts zero elements
2.
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])
easy
A. np.count_nonzero = arr
B. np.count_nonzero(arr)
C. arr.count_nonzero()
D. np.count(arr != 0)
Solution
Step 1: Identify correct function usage
The function np.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 function np.count().
Final Answer:
np.count_nonzero(arr) -> Option B
Quick Check:
Correct syntax is np.count_nonzero(array) [OK]
Hint: Use np.count_nonzero(array) to count non-zero values [OK]