Recall & Review
beginner
What is broadcasting in NumPy?
Broadcasting is a way NumPy performs operations on arrays of different shapes by automatically expanding the smaller array to match the shape of the larger one without copying data.
Click to reveal answer
beginner
What causes a broadcasting error in NumPy?
A broadcasting error happens when two arrays have incompatible shapes that cannot be aligned according to broadcasting rules, so NumPy cannot perform element-wise operations.
Click to reveal answer
beginner
How can you check the shape of a NumPy array?
You can check the shape of a NumPy array using the `.shape` attribute, which returns a tuple showing the size of the array in each dimension.
Click to reveal answer
intermediate
What is a common debugging step when facing a broadcasting error?
A common step is to print the shapes of the arrays involved and compare them to see why they cannot be broadcast together, then reshape or expand dimensions as needed.
Click to reveal answer
intermediate
Explain the broadcasting rule for two arrays with shapes (3, 1) and (1, 4).
NumPy compares shapes from right to left: 1 and 4 are compatible because 1 can be broadcast to 4; 3 and 1 are compatible because 1 can be broadcast to 3. The result shape is (3, 4).
Click to reveal answer
What happens if you try to add arrays with shapes (2, 3) and (3, 2) in NumPy?
✗ Incorrect
Shapes (2, 3) and (3, 2) are not compatible for broadcasting because neither dimension matches or is 1.
Which attribute helps you find the shape of a NumPy array?
✗ Incorrect
The .shape attribute returns the dimensions of the array as a tuple.
If an array has shape (5, 1) and another has shape (1, 4), what is the shape after broadcasting?
✗ Incorrect
Dimensions with 1 can be broadcast to match the other dimension, resulting in (5, 4).
What is a good first step to debug a broadcasting error?
✗ Incorrect
Printing shapes helps identify why arrays cannot be broadcast together.
Which of these shapes can be broadcast together?
✗ Incorrect
Shapes (4,1) and (1,5) broadcast to (4,5) because 1 can be stretched to match the other dimension.
Describe the main rules NumPy uses to decide if two arrays can be broadcast together.
Think about how NumPy stretches arrays to match shapes.
You got /3 concepts.
Explain how you would debug a broadcasting error in your code.
Start by printing array shapes before the operation.
You got /4 concepts.