0
0
NumPydata~30 mins

1D and 2D broadcasting in NumPy - Mini Project: Build & Apply

Choose your learning style9 modes available
1D and 2D broadcasting
📖 Scenario: You work in a small bakery that tracks daily sales of different types of bread. You want to analyze the sales data to understand how many breads were sold each day and how the prices affect total revenue.
🎯 Goal: Build a small program using numpy to apply 1D and 2D broadcasting. You will create arrays for daily sales and prices, then calculate total revenue using broadcasting.
📋 What You'll Learn
Create a 2D numpy array called sales representing the number of breads sold for 3 types over 4 days.
Create a 1D numpy array called prices representing the price of each bread type.
Use broadcasting to calculate the total revenue per day for each bread type.
Print the resulting revenue array.
💡 Why This Matters
🌍 Real World
Broadcasting helps quickly perform operations on sales and price data without writing loops, saving time and reducing errors.
💼 Career
Data analysts and scientists use broadcasting in numpy to efficiently handle and analyze multi-dimensional data like sales, prices, and revenues.
Progress0 / 4 steps
1
Create the sales data array
Create a 2D numpy array called sales with these exact values: [[10, 15, 7], [12, 18, 5], [9, 14, 8], [11, 16, 6]]. This array represents the number of breads sold for 3 types over 4 days.
NumPy
Need a hint?

Use np.array() to create the 2D array with the exact values.

2
Create the prices array
Create a 1D numpy array called prices with these exact values: [2.5, 3.0, 4.0]. This array represents the price of each bread type.
NumPy
Need a hint?

Use np.array() to create the 1D array with the exact prices.

3
Calculate total revenue using broadcasting
Use broadcasting to multiply the sales array by the prices array and store the result in a new variable called revenue. This will calculate the total revenue per bread type for each day.
NumPy
Need a hint?

Simply multiply the 2D sales array by the 1D prices array to use broadcasting.

4
Print the revenue array
Print the revenue array to display the total revenue per bread type for each day.
NumPy
Need a hint?

Use print(revenue) to show the final result.