0
0
NumPydata~30 mins

Aggregation along specific axes in NumPy - Mini Project: Build & Apply

Choose your learning style9 modes available
Aggregation along specific axes
📖 Scenario: You work in a small bakery that tracks daily sales of three types of bread over a week. You want to analyze the total sales per bread type and per day to understand which bread is most popular and which day has the highest sales.
🎯 Goal: Build a program that creates a 2D array of sales data, then calculates the total sales per bread type and per day using aggregation along specific axes.
📋 What You'll Learn
Create a 2D numpy array called sales with exact values representing sales of three bread types over seven days.
Create a variable called axis_bread set to 0 to represent aggregation along bread types.
Use np.sum with axis=axis_bread to calculate total sales per day and store it in total_per_day.
Create a variable called axis_day set to 1 to represent aggregation along days.
Use np.sum with axis=axis_day to calculate total sales per bread type and store it in total_per_bread.
Print total_per_day and total_per_bread to show the results.
💡 Why This Matters
🌍 Real World
Aggregating sales data helps businesses understand trends and make decisions about inventory and marketing.
💼 Career
Data analysts and scientists often aggregate data along specific axes to summarize and interpret multi-dimensional datasets.
Progress0 / 4 steps
1
Create the sales data array
Create a 2D numpy array called sales with these exact values representing sales of three bread types over seven days:
[[10, 12, 9, 11, 13, 8, 7], [5, 7, 6, 8, 7, 5, 6], [3, 4, 2, 5, 4, 3, 2]]
NumPy
Need a hint?

Use np.array and pass the list of lists exactly as shown.

2
Set axis variables for aggregation
Create a variable called axis_bread and set it to 0 to represent aggregation along bread types (rows). Then create a variable called axis_day and set it to 1 to represent aggregation along days (columns).
NumPy
Need a hint?

Just assign the numbers 0 and 1 to the variables axis_bread and axis_day.

3
Calculate total sales per day and per bread type
Use np.sum with axis=axis_bread on sales to calculate total sales per day and store it in total_per_day. Then use np.sum with axis=axis_day on sales to calculate total sales per bread type and store it in total_per_bread.
NumPy
Need a hint?

Use np.sum with the correct axis variables to get sums along rows or columns.

4
Print the results
Print the variables total_per_day and total_per_bread to display the total sales per day and per bread type.
NumPy
Need a hint?

Use two print statements, one for each variable.