Using np.broadcast_to() for Explicit Broadcasting
📖 Scenario: Imagine you are working with sales data for a small store. You have the sales numbers for one product over a week, but you want to compare it with a target sales array that has the same shape as the weekly data for easy calculations.
🎯 Goal: You will create a numpy array for weekly sales, then use np.broadcast_to() to expand a single target sales value to match the weekly sales shape. Finally, you will print the broadcasted target sales array.
📋 What You'll Learn
Create a numpy array called
weekly_sales with the exact values: [10, 15, 20, 25, 30, 35, 40]Create a variable called
target_sales with the exact value 20Use
np.broadcast_to() to create a new array called broadcasted_target that has the same shape as weekly_sales but with all values equal to target_salesPrint the
broadcasted_target array💡 Why This Matters
🌍 Real World
Broadcasting helps when you want to compare or combine data of different shapes without manually repeating values. For example, comparing daily sales to a fixed target.
💼 Career
Data scientists often use broadcasting to efficiently perform operations on arrays of different shapes, saving time and memory.
Progress0 / 4 steps