Recall & Review
beginner
What is broadcasting in numpy?
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?
✗ 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?
✗ Incorrect
Shape (3,) is treated as (1,3), so dimensions are compatible.
What error does numpy raise if arrays are not broadcast compatible?
✗ Incorrect
Numpy raises ValueError for incompatible broadcasting.
What is the broadcasted shape of arrays with shapes (4,1,6) and (3,1)?
✗ 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?
✗ 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.