Broadcasting lets arrays with different shapes work together in math. Sometimes shapes don't match, causing errors. Debugging helps fix these shape problems.
When adding a list of daily temperatures to a list of cities but shapes differ
When multiplying a matrix by a vector and getting a shape error
When trying to combine data from different sources with different sizes
When reshaping data for machine learning and facing unexpected errors
Syntax
NumPy
import numpy as np
# Example of broadcasting error
arr1 = np.array([1, 2, 3])
arr2 = np.array([[1, 2], [3, 4]])
result = arr1 + arr2 # This will NOT cause a broadcasting error
Broadcasting works when array shapes are compatible from the right side.
Errors happen if dimensions don't match or can't be stretched.
Examples
Here, arr1 shape (3,) and arr2 shape (3,1) broadcast to (3,3) without error.
Hint: Compare array shapes dimension-wise from right [OK]
Common Mistakes:
Ignoring that (3,) and (2,2) cannot broadcast
Confusing 1D and 2D shapes
Assuming all arrays add without error
3. What is the output of this code?
import numpy as np
x = np.array([[1, 2, 3], [4, 5, 6]])
y = np.array([10, 20])
result = x + y
print(result)
medium
A. Broadcasting error
B. [[11 12 13]
[14 15 16]]
C. [[11 12 13]
[24 25 26]]
D. [[11 22 13]
[14 25 16]]
Solution
Step 1: Check shapes of x and y
x shape is (2,3), y shape is (2,). These shapes are not compatible for broadcasting because trailing dimensions differ and y cannot be stretched to match x.
Step 2: Predict result of x + y
Since shapes are incompatible, NumPy raises a broadcasting error.
Final Answer:
Broadcasting error -> Option A
Quick Check:
Shape (2,3) + (2,) = error [OK]
Hint: Match shapes from right; trailing dims must be equal or 1 [OK]
Common Mistakes:
Assuming (2,) broadcasts to (2,3)
Expecting element-wise addition without shape check
Ignoring broadcasting rules for 2D arrays
4. You get a broadcasting error when adding arrays a with shape (3, 1) and b with shape (3,). How can you fix it?
medium
A. Reshape a to (3,) before adding
B. No fix needed; shapes are compatible
C. Transpose a to (1, 3) before adding
D. Reshape b to (3, 1) before adding
Solution
Step 1: Analyze shapes
a (3,1), b (3,): pad b to (1,3). Trailing: 1 vs 3 (1 stretches), leading: 3 vs 1 (1 stretches). Compatible, result shape (3,3).
Step 2: No error occurs
Direct addition works fine.
Final Answer:
No fix needed; shapes are compatible -> Option B
Quick Check:
(3,1) + (3,) works [OK]
Hint: Verify compatibility: pad lower-rank with leading 1s [OK]
Common Mistakes:
Thinking (3,1) + (3,) errors
Reshaping when automatic broadcasting works
Confusing padding position
5. You have a 3D array arr with shape (4, 1, 5) and a 2D array mat with shape (1, 3). You want to add them without error. What is the correct way?
hard
A. Add directly; broadcasting works automatically
B. Reshape arr to (4, 3, 5) before adding
C. Reshape mat to (1, 1, 3) before adding
D. Reshape mat to (1, 3, 1) before adding
Solution
Step 1: Check shapes and broadcasting rules
arr shape: (4,1,5), mat shape: (1,3). To add, shapes must be compatible from right: 5 vs missing, 1 vs 3, 4 vs 1.
Step 2: Reshape mat to match arr's dimensions
Reshape mat to (1,3,1) so shapes become (4,1,5) and (1,3,1), compatible for broadcasting.
Final Answer:
Reshape mat to (1, 3, 1) before adding -> Option D
Quick Check:
Match dimensions by adding missing axis [OK]
Hint: Add missing dimensions with reshape for broadcasting [OK]
Common Mistakes:
Assuming direct add works without reshape
Reshaping arr incorrectly
Confusing dimension order in reshape
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.