0
0
Data Analysis Pythondata~15 mins

Broadcasting rules in Data Analysis Python - Mini Project: Build & Apply

Choose your learning style9 modes available
Understanding Broadcasting Rules in NumPy
📖 Scenario: Imagine you work in a small bakery. You want to calculate the total cost of different types of bread sold each day. You have the number of breads sold and the price per bread type. You want to multiply these two lists to get the total sales for each bread type.
🎯 Goal: Learn how to use NumPy broadcasting rules to multiply arrays of different shapes to calculate total sales.
📋 What You'll Learn
Create a NumPy array called bread_sold with the exact values [10, 15, 7] representing the number of breads sold for three types.
Create a NumPy array called price_per_bread with the exact values [2, 3, 4] representing the price per bread type.
Use broadcasting to multiply bread_sold and price_per_bread to get total sales per bread type in a variable called total_sales.
Print the total_sales array.
💡 Why This Matters
🌍 Real World
Broadcasting is used in data science to perform fast and memory-efficient operations on arrays of different shapes, like sales data, sensor readings, or image processing.
💼 Career
Understanding broadcasting helps you write cleaner and faster code for data analysis tasks, a key skill for data scientists and analysts.
Progress0 / 4 steps
1
Create the bread sold array
Import NumPy as np and create a NumPy array called bread_sold with the exact values [10, 15, 7].
Data Analysis Python
Need a hint?

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

2
Create the price per bread array
Create a NumPy array called price_per_bread with the exact values [2, 3, 4].
Data Analysis Python
Need a hint?

Use np.array() again to create the price array.

3
Calculate total sales using broadcasting
Use broadcasting to multiply bread_sold and price_per_bread element-wise and store the result in a variable called total_sales.
Data Analysis Python
Need a hint?

Simply multiply the two arrays using the * operator.

4
Print the total sales
Print the total_sales array to see the total sales for each bread type.
Data Analysis Python
Need a hint?

Use print(total_sales) to display the result.