📖 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
1
Create the sales data array
Import NumPy as np and create a NumPy array called sales with these exact values and shape (7, 5): [[100, 200, 300, 400, 500], [110, 210, 310, 410, 510], [120, 220, 320, 420, 520], [130, 230, 330, 430, 530], [140, 240, 340, 440, 540], [150, 250, 350, 450, 550], [160, 260, 360, 460, 560]]
NumPy
Hint
Use np.array([...]) with the exact nested list of values.
2
Create the discount array
Create a NumPy array called discount with these exact values: [0.9, 0.8, 0.85, 0.95, 0.9] representing discount factors for each product.
NumPy
Hint
Use np.array([...]) with the exact discount values.
3
Apply the discount using broadcasting
Create a new NumPy array called discounted_sales by multiplying sales with discount using broadcasting. Use the * operator directly.
NumPy
Hint
Use the * operator to multiply sales and discount arrays directly.
4
Print the discounted sales
Print the discounted_sales array to see the final discounted sales values.
NumPy
Hint
Use print(discounted_sales) to display the array.
Practice
(1/5)
1. What does broadcasting in NumPy allow you to do?
easy
A. Increase the size of arrays by duplicating elements
B. Automatically convert data types during operations
C. Prevent any shape mismatch errors in arrays
D. Perform operations on arrays of different shapes if compatible
Solution
Step 1: Understand broadcasting concept
Broadcasting allows NumPy to perform element-wise operations on arrays with different shapes if they follow certain compatibility rules.
Step 2: Identify correct description
Perform operations on arrays of different shapes if compatible correctly describes broadcasting as working with different shapes if compatible. Other options describe unrelated behaviors.
Final Answer:
Perform operations on arrays of different shapes if compatible -> Option D
Quick Check:
Broadcasting = operations on compatible shapes [OK]
Hint: Broadcasting works only if shapes are compatible [OK]
Common Mistakes:
Thinking broadcasting changes data types
Assuming broadcasting duplicates array elements
Believing broadcasting prevents all errors
2. Which of the following NumPy operations will cause a broadcasting error?
A. arr1 and arr2 have incompatible shapes for broadcasting
B. arr2 should be reshaped to (2,1) before addition
C. arr1 should be flattened before addition
D. No mistake; code runs without error
Solution
Step 1: Check shapes of arr1 and arr2
arr1 shape is (2,2), arr2 shape is (3,). These shapes are not compatible for broadcasting.
Step 2: Understand broadcasting rules
For broadcasting, trailing dimensions must be equal or one must be 1. Here, 2 vs 3 mismatch causes error.
Final Answer:
arr1 and arr2 have incompatible shapes for broadcasting -> Option A
Quick Check:
Shape mismatch causes error = arr1 and arr2 have incompatible shapes for broadcasting [OK]
Hint: Check trailing dimensions match or are 1 [OK]
Common Mistakes:
Assuming arrays with different lengths always broadcast
Trying to flatten arrays unnecessarily
Ignoring shape of second array
5. You have a 3D array data with shape (4, 3, 2) representing 4 samples, 3 features, and 2 measurements each. You want to add a 1D array offset of shape (2,) to each measurement across all samples and features without errors. Which code correctly avoids broadcasting mistakes?
hard
A. result = data + offset.reshape(1, 1, 2)
B. result = data + offset.reshape(4, 3, 2)
C. result = data + offset.reshape(2, 1, 1)
D. result = data + offset.reshape(2, 1)
Solution
Step 1: Understand shapes and broadcasting
data shape is (4,3,2), offset shape is (2,). To broadcast across samples (4) and features (3) matching measurements (2), need compatible shape.
Step 2: Reshape offset to (1,1,2) to broadcast properly
Reshaping offset to (1,1,2) aligns dimensions: 1->4, 1->3, 2=2.
Step 3: Check other options
A: reshape(2,1) to (2,1), aligns as (2,1) vs (4,3,2): dimension mismatch. B: reshape(4,3,2) ValueError (size 2≠24). C: reshape(2,1,1) (2,1,1) vs (4,3,2): dim0 2≠4 incompatible.
Final Answer:
result = data + offset.reshape(1, 1, 2) -> Option A
Quick Check:
Reshape offset to match trailing dimension [OK]
Hint: Reshape 1D array to (1,1,n) for 3D addition [OK]
Common Mistakes:
Reshaping to wrong dimensions like (2,1)
Reshaping to full data shape unnecessarily
Trying to reshape to (2,1,1) mismatching leading dimensions
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.