Broadcasting lets us do math on arrays of different shapes easily. But if shapes don't match well, results can be wrong. Avoiding mistakes helps get correct answers.
Adding a list of numbers to a table of data with different rows and columns
Multiplying a single number or row by a whole matrix
Applying a function to each row or column without writing loops
Combining arrays from different sources that may have different shapes
Syntax
NumPy
result = array1 + array2
Arrays must have compatible shapes for broadcasting.
Broadcasting compares shapes from the right side, matching dimensions or using 1.
Examples
Adding a 1D array to each row of a 2D array works because shapes (2,3) and (3,) are compatible.
NumPy
import numpy as np
# Correct broadcasting
A = np.array([[1, 2, 3], [4, 5, 6]])
B = np.array([10, 20, 30])
C = A + B
print(C)
Shapes (2,2) and (3,) do not match, so broadcasting fails and raises an error.
NumPy
import numpy as np
# Mistake: incompatible shapes
A = np.array([[1, 2], [3, 4]])
B = np.array([10, 20, 30])
C = A + B # This will cause an error
Reshaping B to (2,1) allows broadcasting with A's shape (2,2).
NumPy
import numpy as np
# Using reshape to fix shape
A = np.array([[1, 2], [3, 4]])
B = np.array([10, 20])
C = A + B.reshape(2, 1)
print(C)
Sample Program
This program shows adding a vector to each row of a matrix correctly, then tries a wrong shape causing an error, and finally fixes it by reshaping.
NumPy
import numpy as np
# Define a 2D array with 3 rows and 2 columns
matrix = np.array([[1, 2], [3, 4], [5, 6]])
# Define a 1D array with 2 elements
vector = np.array([10, 20])
# Add vector to each row of matrix (correct broadcasting)
result_correct = matrix + vector
print("Correct broadcasting result:")
print(result_correct)
# Define a 1D array with 3 elements (wrong shape for broadcasting)
wrong_vector = np.array([10, 20, 30])
try:
# This will raise an error because shapes don't match
result_wrong = matrix + wrong_vector
except ValueError as e:
print("Broadcasting error:", e)
# Fix by reshaping wrong_vector to (3,1)
fixed_vector = wrong_vector.reshape(3, 1)
result_fixed = matrix + fixed_vector
print("Fixed broadcasting result:")
print(result_fixed)
OutputSuccess
Important Notes
Always check array shapes before operations to avoid silent mistakes.
Use array.shape to see dimensions.
Reshape arrays with reshape() to make shapes compatible.
Summary
Broadcasting lets arrays with different shapes work together if compatible.
Shape mismatch causes errors or wrong results.
Check and fix shapes before math operations to avoid mistakes.
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.