Broadcasting is a way numpy allows arrays of different shapes to be used together in arithmetic operations by automatically expanding their shapes to be compatible.
Click to reveal answer
intermediate
How does numpy handle broadcasting when arrays have different numbers of dimensions?
Numpy adds dimensions of size 1 to the smaller array on its left side until both arrays have the same number of dimensions, then compares shapes dimension-wise for compatibility.
Click to reveal answer
intermediate
Given arrays with shapes (3, 1, 5) and (1, 4, 1), what will be the broadcasted shape?
The broadcasted shape will be (3, 4, 5). Numpy expands dimensions of size 1 to match the other array's size in that dimension.
Click to reveal answer
beginner
Why is broadcasting useful when working with higher dimensional data?
Broadcasting lets you perform operations on arrays without manually reshaping or repeating data, saving memory and making code simpler and faster.
Click to reveal answer
beginner
What error occurs if numpy arrays cannot be broadcast together?
Numpy raises a ValueError saying 'operands could not be broadcast together' when their shapes are incompatible for broadcasting.
Click to reveal answer
If array A has shape (2, 3, 1) and array B has shape (3, 4), what shape will numpy broadcast them to?
A(3, 3, 4)
B(2, 3, 4)
C(2, 3, 3, 4)
DBroadcasting error
✗ Incorrect
Numpy adds a dimension to B to make it (1, 3, 4), then broadcasts to (2, 3, 4).
What does numpy do if one array has fewer dimensions than the other during broadcasting?
AAdds dimensions of size 1 to the right
BRaises an error immediately
CAdds dimensions of size 1 to the left
DIgnores the extra dimensions
✗ Incorrect
Numpy adds dimensions of size 1 to the left side of the smaller array.
Which of these pairs of shapes can be broadcast together?
A(4, 1, 3) and (1, 5, 3)
B(2, 3) and (3, 2)
C(3, 4) and (3, 4, 1)
D(5, 2) and (5, 3)
✗ Incorrect
Shapes (4,1,3) and (1,5,3) broadcast to (4,5,3). Others are incompatible.
What is the main benefit of broadcasting with higher dimensions?
AIt reduces the need for loops and manual reshaping
BIt increases memory usage
CIt slows down computations
DIt requires arrays to be the same shape
✗ Incorrect
Broadcasting lets numpy perform operations efficiently without loops or manual reshaping.
If you try to add arrays of shapes (3, 2) and (2, 3), what happens?
AThey broadcast to (2, 3)
BThey broadcast to (3, 3)
CThey broadcast to (3, 2, 3)
DValueError: operands could not be broadcast together
✗ Incorrect
Shapes (3, 2) and (2, 3) are incompatible for broadcasting, so numpy raises a ValueError.
Explain how numpy broadcasts arrays with different numbers of dimensions. Include how dimensions are added and how shapes are compared.
Think about how numpy aligns shapes before arithmetic.
You got /4 concepts.
Describe a real-life example where broadcasting with higher dimensions can simplify data operations.
Think about working with batches of data or repeated operations.
You got /4 concepts.
Practice
(1/5)
1. What does broadcasting in NumPy allow you to do when working with arrays of different shapes?
easy
A. Copy data from smaller arrays to larger arrays explicitly
B. Perform element-wise operations by stretching smaller arrays to match larger ones
C. Only work with arrays of the exact same shape
D. Convert arrays into lists automatically
Solution
Step 1: Understand broadcasting concept
Broadcasting lets NumPy perform operations on arrays with different shapes by stretching the smaller array without copying data.
Step 2: Compare options with concept
Only Perform element-wise operations by stretching smaller arrays to match larger ones correctly describes this behavior. Options B, C, and D are incorrect because broadcasting does not copy data explicitly, does not require same shape, and does not convert arrays to lists.
Final Answer:
Perform element-wise operations by stretching smaller arrays to match larger ones -> Option B
Quick Check:
Broadcasting = Stretch smaller arrays [OK]
Hint: Broadcasting stretches smaller arrays to match bigger ones [OK]
Common Mistakes:
Thinking broadcasting copies data
Believing arrays must have same shape
Confusing broadcasting with data type conversion
2. Which of the following NumPy array shapes can be broadcast together for element-wise addition?
easy
A. (4, 5) and (4, 5, 1)
B. (2, 3) and (3, 2)
C. (3, 1, 5) and (1, 4, 5)
D. (3, 3) and (2, 3)
Solution
Step 1: Check broadcasting rules for each dimension
Shapes (3,1,5) and (1,4,5) can broadcast because dimensions are compatible: 3 vs 1, 1 vs 4, 5 vs 5.
Step 2: Verify other options
Options A, B, and D have incompatible dimensions that cannot be broadcast for element-wise addition.
Hint: Match dimensions from right, 1 or equal size broadcasts [OK]
Common Mistakes:
Ignoring dimension order when checking compatibility
Assuming all different shapes can broadcast
Confusing shape sizes in different axes
3. What is the output of this code?
import numpy as np
A = np.ones((2, 3, 1))
B = np.arange(4).reshape(1, 1, 4)
C = A + B
print(C.shape)
medium
A. ValueError due to shape mismatch
B. (2, 3, 1)
C. (1, 1, 4)
D. (2, 3, 4)
Solution
Step 1: Analyze shapes of A and B
A has shape (2, 3, 1), B has shape (1, 1, 4). Broadcasting rules allow these to combine because 1 can stretch to 2 and 3, and 1 can stretch to 4.
Step 2: Determine resulting shape
The result shape is the maximum along each dimension: max(2,1)=2, max(3,1)=3, max(1,4)=4, so (2, 3, 4).
Final Answer:
(2, 3, 4) -> Option D
Quick Check:
Broadcast result shape = (2,3,4) [OK]
Hint: Result shape = max dimension sizes per axis [OK]
Common Mistakes:
Confusing which dimension broadcasts to which
Expecting error instead of broadcasting
Mixing up shape order
4. Identify the error in this code snippet:
import numpy as np
X = np.ones((3, 1, 2))
Y = np.ones((2, 3))
Z = X + Y
medium
A. Shapes (3,1,2) and (2,3) cannot be broadcast together
B. Y should be reshaped to (1, 2, 3) before addition
C. X and Y have incompatible data types
D. No error, code runs fine
Solution
Step 1: Compare shapes for broadcasting
X shape is (3,1,2), Y shape is (2,3). Aligning from right: 2 vs 3 (mismatch), 1 vs 2 (mismatch), 3 vs missing dimension.
Step 2: Conclude broadcasting compatibility
Dimensions do not match or equal 1, so broadcasting fails and raises an error.
Final Answer:
Shapes (3,1,2) and (2,3) cannot be broadcast together -> Option A
Quick Check:
Broadcast error due to incompatible shapes [OK]
Hint: Check shapes right to left; mismatch means error [OK]
Common Mistakes:
Assuming automatic reshaping without explicit code
Ignoring dimension order in broadcasting
Thinking data types cause this error
5. You have a 3D array data with shape (10, 1, 5) representing 10 samples, 1 channel, and 5 features. You want to add a bias vector of shape (5,) to each sample and channel. Which code correctly applies broadcasting to add the bias to data?
hard
A. bias = np.array([1,2,3,4,5])
data + bias
B. bias = np.array([[1,2,3,4,5]])
data + bias
C. bias = np.array([1,2,3,4,5]).reshape((5,1))
data + bias
D. bias = np.array([[[1,2,3,4,5]]])
data + bias
Solution
Step 1: Understand required bias shape and broadcasting
data shape (10,1,5). Bias vector shape (5,) pads with 1s on left to (1,1,5) and broadcasts correctly across samples and channels.
Step 2: Evaluate each option
A: bias shape (5,), correct. B: shape (1,5) also broadcasts correctly, but is redundant. C: shape (5,1) incompatible. D: shape (1,1,5) also broadcasts correctly, but is more verbose.
Final Answer:
bias = np.array([1,2,3,4,5]) data + bias -> Option A
Quick Check:
(10,1,5) + (5,) -> broadcasts OK
Hint: Bias shape must match trailing dims with extra dims as 1 [OK]
Common Mistakes:
Using 1D bias without extra dimensions
Wrong reshape causing shape mismatch
Assuming automatic dimension expansion without reshape
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.