Recall & Review
beginner
What is broadcasting in numpy?
Broadcasting is numpy's way to perform operations on arrays of different shapes by automatically expanding the smaller array to match the larger one without copying data.
Click to reveal answer
beginner
Why can broadcasting cause mistakes in numpy calculations?
Broadcasting can cause mistakes when arrays have shapes that seem compatible but lead to unintended expansion, causing wrong results or hard-to-find bugs.
Click to reveal answer
beginner
How can you check the shapes of arrays before broadcasting?
You can print the shapes using the `.shape` attribute of numpy arrays to ensure they align as expected before performing operations.
Click to reveal answer
intermediate
What is a safe way to avoid broadcasting mistakes when adding arrays?
Make sure arrays have compatible shapes or explicitly reshape arrays using `.reshape()` or `np.expand_dims()` to control how broadcasting happens.
Click to reveal answer
beginner
What happens if you try to broadcast arrays with incompatible shapes?
Numpy raises a ValueError indicating the shapes are not compatible for broadcasting, preventing silent wrong calculations.
Click to reveal answer
What does numpy do when you add a (3,1) array to a (3,4) array?
✗ Incorrect
Numpy broadcasts the smaller dimension (1) to match 4, so (3,1) becomes (3,4) for element-wise addition.
Which shape pair will cause a broadcasting error?
✗ Incorrect
Shapes (3,2) and (2,3) are incompatible for broadcasting because their dimensions do not align from the right.
How can you avoid unexpected broadcasting results?
✗ Incorrect
Checking shapes helps ensure arrays will broadcast as intended, avoiding mistakes.
What numpy method helps to add a new axis for broadcasting?
✗ Incorrect
np.expand_dims() adds a new axis, helping control broadcasting behavior.
If you want to add a (3,) array to each row of a (3,4) array, what should you do?
✗ Incorrect
Reshaping (3,) to (3,1) aligns dimensions for broadcasting across columns.
Explain what broadcasting is and why it can cause mistakes in numpy.
Think about how numpy handles arrays of different shapes during operations.
You got /4 concepts.
Describe steps you can take to avoid broadcasting mistakes when working with numpy arrays.
Consider how to prepare arrays before operations.
You got /4 concepts.