0
0
NumPydata~30 mins

Broadcasting rules in NumPy - Mini Project: Build & Apply

Choose your learning style9 modes available
Understanding Broadcasting Rules with NumPy
📖 Scenario: Imagine you are working with sales data for a small store. You have daily sales numbers for different products, and you want to apply a discount rate to all products easily using NumPy arrays.
🎯 Goal: You will learn how to use NumPy broadcasting rules to apply a discount rate to a 2D array of sales data without writing loops.
📋 What You'll Learn
Create a NumPy 2D array with exact sales data
Create a NumPy 1D array with exact discount rates
Use broadcasting to apply discounts to sales data
Print the discounted sales array
💡 Why This Matters
🌍 Real World
Applying discounts or adjustments to sales data is common in retail and business analytics. Broadcasting helps apply these changes efficiently without writing loops.
💼 Career
Data scientists and analysts often use broadcasting in NumPy to perform fast and readable calculations on large datasets.
Progress0 / 4 steps
1
Create the sales data array
Import NumPy as np and create a 2D NumPy array called sales with these exact values: [[100, 200, 300], [400, 500, 600]].
NumPy
Need a hint?

Use np.array() to create the array with the exact nested list.

2
Create the discount rates array
Create a 1D NumPy array called discounts with these exact values: [0.9, 0.8, 0.7] representing discount rates for each product.
NumPy
Need a hint?

Create a 1D array with the discount rates matching the number of columns in sales.

3
Apply broadcasting to calculate discounted sales
Create a new NumPy array called discounted_sales by multiplying sales with discounts using broadcasting.
NumPy
Need a hint?

Simply multiply the 2D sales array by the 1D discounts array to apply broadcasting.

4
Print the discounted sales array
Print the discounted_sales array to see the result of applying the discounts.
NumPy
Need a hint?

Use print(discounted_sales) to display the array.