0
0
NumPydata~5 mins

Common broadcasting patterns in NumPy - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
ABoth arrays have the same number of elements
BDimensions are equal or one is 1
CArrays have the same data type
DArrays have the same number of dimensions
What is the shape of the result when adding arrays of shapes (4,1) and (1,5)?
A(1,5)
B(1,1)
C(4,1)
D(4,5)
If you add a scalar to an array of shape (3,3), what is the shape of the result?
A(3,1)
B(1,3)
C(3,3)
D()
What happens if you try to broadcast arrays with shapes (2,3) and (3,2)?
ABroadcasting fails due to incompatible shapes
BBroadcasting succeeds with shape (2,3,3,2)
CBroadcasting succeeds with shape (3,3)
DBroadcasting succeeds with shape (2,2)
Which broadcasting pattern is used when multiplying arrays of shapes (3,1) and (1,4)?
AResult shape is (3,4) by expanding both arrays
BResult shape is (3,1)
CResult shape is (1,4)
DMultiplication is not possible
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.