0
0
NumPydata~15 mins

np.count_nonzero() for counting in NumPy - Mini Project: Build & Apply

Choose your learning style9 modes available
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
Need a 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
Need a 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
Need a 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
Need a hint?

Use print(products_sold) to display the count.