📖 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
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
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
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
Hint
Use print(total_sales) to display the result.
Practice
(1/5)
1. What does broadcasting do when working with arrays of different shapes in Python's NumPy?
easy
A. It stretches smaller arrays to match the shape of larger arrays without copying data.
B. It copies smaller arrays to make them the same size as larger arrays.
C. It only works if arrays have exactly the same shape.
D. It deletes extra elements from larger arrays to match smaller ones.
Solution
Step 1: Understand broadcasting concept
Broadcasting allows operations on arrays with different shapes by stretching the smaller array.
Step 2: Identify how broadcasting works
It does not copy data but virtually stretches the smaller array to match the larger one.
Final Answer:
It stretches smaller arrays to match the shape of larger arrays without copying data. -> Option A
Quick Check:
Broadcasting = stretching without copying [OK]
Hint: Broadcasting stretches smaller arrays without copying data [OK]
Common Mistakes:
Thinking broadcasting copies data
Believing arrays must have same shape
Assuming larger arrays get shrunk
2. Which of the following code snippets correctly applies broadcasting to add a 1D array to each row of a 2D array in NumPy?
easy
A. result = arr2d + arr1d[:, np.newaxis]
B. result = arr2d.flatten() + arr1d
C. result = arr2d + arr1d.reshape(-1, 1)
D. result = arr2d + arr1d
Solution
Step 1: Understand shapes of arrays
arr2d is 2D (m,n), arr1d is 1D (n,). Adding arr1d directly to arr2d adds arr1d to each row by broadcasting.
Step 2: Check reshaping effects
Reshaping arr1d to column vector (A,C) causes shape incompatibility (m,n) + (n,1); flattening arr2d (D) leads to incompatible 1D shapes.
Final Answer:
result = arr2d + arr1d -> Option D
Quick Check:
Direct addition broadcasts 1D to rows [OK]
Hint: Add 1D array directly to 2D to broadcast rows [OK]
Hint: Check trailing dimensions for broadcasting compatibility [OK]
Common Mistakes:
Assuming any shapes can broadcast
Thinking reshaping arr2 fixes without correct shape
Believing '+' can't add arrays
5. You have a 3D array arr3d with shape (2, 3, 4) and a 1D array arr1d with shape (4,). You want to add arr1d to each 2D slice of arr3d along the last axis. Which code correctly uses broadcasting to do this?
hard
A. result = arr3d + arr1d.reshape(2, 3, 4)
B. result = arr3d + arr1d.reshape(1, 1, 4)
C. result = arr3d + arr1d.reshape(4, 1, 1)
D. result = arr3d + arr1d.reshape(1, 4, 1)
Solution
Step 1: Understand shapes and broadcasting rules
arr3d shape is (2,3,4), arr1d shape is (4,). To add along last axis, arr1d must broadcast to (1,1,4).
Step 2: Reshape arr1d for correct broadcasting
Reshape arr1d to (1,1,4) so it broadcasts across first two dimensions and aligns with last dimension.
Step 3: Confirm addition
Addition with arr3d + arr1d.reshape(1,1,4) works correctly by broadcasting arr1d along first two axes.
Final Answer:
result = arr3d + arr1d.reshape(1, 1, 4) -> Option B
Quick Check:
Reshape 1D to (1,1,last_dim) for 3D broadcasting [OK]
Hint: Reshape 1D to (1,1,last_dim) to add along last axis [OK]
Common Mistakes:
Not reshaping arr1d or wrong reshape shape
Confusing axis order in reshape
Assuming direct addition always works
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.