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 the smaller array along the missing dimensions.
Click to reveal answer
beginner
Explain the rule: When does broadcasting work between two arrays?
Broadcasting works when for each dimension, the sizes are equal or one of them is 1. NumPy stretches the dimension with size 1 to match the other.
Click to reveal answer
beginner
What happens when you add a 1D array of shape (3,) to a 2D array of shape (2,3)?
The 1D array is broadcasted along the first dimension to match the 2D array shape (2,3), so each row adds the 1D array values.
Click to reveal answer
beginner
How does broadcasting handle a scalar value with an array?
A scalar is treated as an array with shape (), so it broadcasts to the shape of the array, applying the scalar value to every element.
Click to reveal answer
intermediate
What is the broadcasting pattern when multiplying a (3,1) array with a (1,4) array?
The (3,1) array is broadcasted along the second dimension and the (1,4) array along the first dimension, resulting in a (3,4) array where each element is the product of corresponding broadcasted values.
Click to reveal answer
Which condition must be true for broadcasting to work between two arrays?
✗ Incorrect
Broadcasting requires that for each dimension, the sizes are equal or one of them is 1.
What is the shape of the result when adding arrays of shapes (4,1) and (1,5)?
✗ Incorrect
The arrays broadcast to shape (4,5) by expanding the dimensions with size 1.
If you add a scalar to an array of shape (3,3), what is the shape of the result?
✗ Incorrect
The scalar broadcasts to the array shape, so the result has the same shape as the array.
What happens if you try to broadcast arrays with shapes (2,3) and (3,2)?
✗ Incorrect
Broadcasting fails because dimensions are not equal and neither is 1 in corresponding positions.
Which broadcasting pattern is used when multiplying arrays of shapes (3,1) and (1,4)?
✗ Incorrect
Both arrays broadcast to shape (3,4), multiplying each element accordingly.
Describe how NumPy broadcasting works with an example of adding a (2,3) array to a (3,) array.
Think about how the smaller array is stretched to match the larger array's shape.
You got /4 concepts.
Explain why broadcasting fails when trying to operate on arrays with shapes (2,3) and (3,2).
Check each dimension from the right and see if they are equal or one is 1.
You got /4 concepts.