0
0
NumPydata~30 mins

np.sum() and axis parameter in NumPy - Mini Project: Build & Apply

Choose your learning style9 modes available
Summing Data with np.sum() and the axis Parameter
📖 Scenario: Imagine you work at a small store that tracks daily sales of different products. You have sales data for 3 products over 4 days. You want to find total sales for each product and total sales for each day.
🎯 Goal: You will create a 2D numpy array with sales data, then use np.sum() with the axis parameter to calculate total sales per product and per day.
📋 What You'll Learn
Create a numpy array called sales with the exact data given.
Create a variable called axis_0 set to 0 to represent summing over rows (days).
Use np.sum() with axis=axis_0 to find total sales per product.
Print the total sales per product.
Create a variable called axis_1 set to 1 to represent summing over columns (products).
Use np.sum() with axis=axis_1 to find total sales per day.
Print the total sales per day.
💡 Why This Matters
🌍 Real World
Stores and businesses often track sales data in tables and need to quickly find totals by product or by day to understand performance.
💼 Career
Data analysts and scientists use numpy and its sum function with axis to summarize and analyze multi-dimensional data efficiently.
Progress0 / 4 steps
1
Create the sales data array
Create a numpy array called sales with this exact 2D data:
[[5, 3, 2], [6, 7, 1], [4, 5, 3], [8, 2, 4]] representing sales of 3 products over 4 days.
NumPy
Need a hint?

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

2
Set axis variables for summing
Create a variable called axis_0 and set it to 0 to sum over rows (days). Then create a variable called axis_1 and set it to 1 to sum over columns (products).
NumPy
Need a hint?

Remember, axis 0 means down the rows, axis 1 means across the columns.

3
Calculate total sales per product and per day
Use np.sum() with axis=axis_0 on sales to calculate total sales per product and save it in total_per_product. Then use np.sum() with axis=axis_1 on sales to calculate total sales per day and save it in total_per_day.
NumPy
Need a hint?

Use np.sum() with the correct axis variable for each total.

4
Print the total sales results
Print total_per_product and then print total_per_day to show the sums.
NumPy
Need a hint?

Use two print() statements, one for each total array.