0
0
NumPydata~5 mins

1D and 2D broadcasting 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 operations between arrays of different shapes by automatically expanding the smaller array to match the larger one without copying data.
Click to reveal answer
beginner
How does 1D broadcasting work when adding a 1D array to a 2D array?
The 1D array is stretched across the matching dimension of the 2D array so each row or column adds the 1D array element-wise.
Click to reveal answer
intermediate
What shape rules must be met for broadcasting to work?
Starting from the trailing dimensions, the sizes must be equal or one of them must be 1 for each dimension.
Click to reveal answer
intermediate
Example: What happens when you add a (3,1) array to a (1,4) array?
They broadcast to a (3,4) array by stretching the first array across columns and the second across rows, then adding element-wise.
Click to reveal answer
beginner
Why is broadcasting useful in data science?
It allows fast, memory-efficient operations on arrays without explicit loops, making code simpler and faster.
Click to reveal answer
What does numpy broadcasting allow you to do?
AOperate on arrays of different shapes without explicit loops
BConvert arrays to lists automatically
CSort arrays in place
DChange array data types automatically
If you add a (3,) array to a (3,4) array, how does broadcasting work?
AThe (3,4) array is reshaped to (3,)
BThe (3,) array is broadcast across each row
CBroadcasting is not possible
DThe (3,) array is broadcast across each column
Which shape pair can broadcast together?
A(2,3) and (3,)
B(2,3) and (2,2)
C(3,1) and (4,)
D(3,2) and (3,3)
What happens if broadcasting rules are not met?
AOperation returns None
BNumpy silently reshapes arrays
CArrays are converted to lists
DNumpy raises a ValueError
Which numpy function can help check array shapes before broadcasting?
Anumpy.shape
Bnumpy.reshape
Cnumpy.broadcast_shapes
Dnumpy.concatenate
Explain in your own words how 1D and 2D arrays can be added using broadcasting in numpy.
Think about how numpy matches dimensions from the end and stretches arrays.
You got /4 concepts.
    Describe the rules numpy uses to decide if two arrays can be broadcast together.
    Focus on how numpy compares each dimension starting from the right.
    You got /3 concepts.