Broadcasting is a way numpy allows arrays of different shapes to work together in arithmetic operations by automatically expanding their shapes to be compatible.
Click to reveal answer
intermediate
How does numpy check if two arrays are broadcast compatible?
Numpy compares their shapes from right to left. For each dimension, the sizes must be equal or one of them must be 1. If this holds for all dimensions, arrays are broadcast compatible.
Click to reveal answer
beginner
Example: Are arrays with shapes (3, 1) and (1, 4) broadcast compatible?
Yes. Comparing from right: 1 and 4 (one is 1), 3 and 1 (one is 1). So they can broadcast to shape (3, 4).
Click to reveal answer
beginner
What happens if arrays are not broadcast compatible?
Numpy raises a ValueError indicating operands could not be broadcast together because their shapes are incompatible.
Click to reveal answer
intermediate
How can you programmatically check broadcasting compatibility in numpy?
You can try to use numpy.broadcast_shapes(shape1, shape2) which returns the broadcasted shape if compatible, or raises an error if not.
Click to reveal answer
Which condition must be true for each dimension when checking broadcasting compatibility?
ADimensions are both prime numbers
BDimensions differ by exactly 1
CDimensions are both even numbers
DDimensions are equal or one is 1
✗ Incorrect
Broadcasting requires each dimension to be equal or one of them to be 1.
Are arrays with shapes (5, 3) and (3,) broadcast compatible?
AOnly if 5 equals 3
BNo
CYes
DOnly if both have same number of dimensions
✗ Incorrect
Shape (3,) is treated as (1,3), so dimensions are compatible.
What error does numpy raise if arrays are not broadcast compatible?
AValueError
BTypeError
CIndexError
DKeyError
✗ Incorrect
Numpy raises ValueError for incompatible broadcasting.
What is the broadcasted shape of arrays with shapes (4,1,6) and (3,1)?
ANot compatible
B(4,1,6)
C(3,1,6)
D(4,3,6)
✗ Incorrect
Arrays with shapes (4,1,6) and (3,1) are not broadcast compatible because after padding (3,1) to (1,3,1), the second dimension comparison is 1 vs 3 (compatible), but the first dimension comparison is 4 vs 1 (compatible), but the third dimension comparison is 6 vs 1 (compatible). However, the second dimension 1 vs 3 is compatible, but the first dimension 4 vs 1 is compatible, so the broadcasted shape would be (4,3,6). Actually, the original explanation was incorrect because (3,1) padded to (1,3,1) compared to (4,1,6) yields dimensions: 4 vs 1 (compatible), 1 vs 3 (compatible), 6 vs 1 (compatible), so they are compatible and broadcasted shape is (4,3,6). Therefore, the correct answer is A.
Which numpy function can be used to check broadcast compatibility of shapes?
Anumpy.check_broadcast
Bnumpy.broadcast_shapes
Cnumpy.shape_compat
Dnumpy.broadcast_check
✗ Incorrect
numpy.broadcast_shapes returns the broadcasted shape or raises an error if incompatible.
Explain how numpy determines if two arrays can be broadcast together.
Think about matching dimensions starting from the end.
You got /4 concepts.
Describe a real-life example where broadcasting helps in data science calculations.
Consider adding a list of values to multiple data rows.
You got /4 concepts.
Practice
(1/5)
1. Which of the following pairs of shapes are compatible for broadcasting in NumPy?
easy
A. (1, 2, 3) and (2, 3, 1)
B. (2, 3) and (3, 2)
C. (4, 5) and (4, 5, 1)
D. (3, 1) and (1, 4)
Solution
Step 1: Understand broadcasting rules
Broadcasting compares shapes from right to left. Dimensions must be equal or one must be 1.
Step 2: Check each pair
For (3,1) and (1,4), rightmost dims 1 and 4 are compatible (1 can broadcast to 4), left dims 3 and 1 compatible (1 can broadcast to 3). So compatible.
Final Answer:
(3, 1) and (1, 4) are compatible -> Option D
Quick Check:
Broadcast dims right to left, 1 can expand [OK]
Hint: Check shapes from right; 1 or equal dims broadcast [OK]
Common Mistakes:
Assuming all different shapes are compatible
Not comparing dimensions from right to left
Ignoring that 1 can broadcast to any size
2. Which of the following is the correct way to check broadcasting compatibility of shapes (2, 3) and (3,) using NumPy?
easy
A. numpy.broadcast_shapes((2, 3), (3,))
B. numpy.broadcast((2, 3), (3,))
C. numpy.broadcast_shapes([2, 3], 3)
D. numpy.broadcast((2, 3), (3))
Solution
Step 1: Identify the correct function
NumPy provides numpy.broadcast_shapes to check shape compatibility, which takes tuples as shape arguments.
Step 2: Check argument types
numpy.broadcast_shapes((2, 3), (3,)) uses numpy.broadcast_shapes with tuples, which is correct. numpy.broadcast_shapes([2, 3], 3) uses a scalar for the second argument, which is invalid for this function.
Final Answer:
numpy.broadcast_shapes((2, 3), (3,)) -> Option A
Quick Check:
Use numpy.broadcast_shapes with tuples [OK]
Hint: Use numpy.broadcast_shapes with tuples for shape check [OK]
Common Mistakes:
Using numpy.broadcast instead of numpy.broadcast_shapes