Avoiding broadcasting mistakes
📖 Scenario: Imagine you are analyzing sales data for a small store. You have daily sales numbers for 5 products over a week. You want to apply a discount to each product's sales but must be careful to avoid mistakes caused by incorrect array shapes in calculations.
🎯 Goal: You will create a NumPy array for sales data, define a discount array, apply the discount correctly using broadcasting, and print the discounted sales.
📋 What You'll Learn
Create a NumPy array called
sales with shape (7, 5) representing 7 days and 5 products.Create a NumPy array called
discount with shape (5,) representing discount factors for each product.Apply the discount to the sales data correctly using broadcasting.
Print the resulting discounted sales array.
💡 Why This Matters
🌍 Real World
Retail stores often apply discounts to products and need to calculate updated sales or revenue correctly using arrays of data.
💼 Career
Data analysts and scientists must understand broadcasting to manipulate multi-dimensional data without errors in Python using NumPy.
Progress0 / 4 steps