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
✗ Incorrect
Broadcasting lets numpy perform operations on arrays of different shapes by expanding the smaller array 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
✗ Incorrect
Broadcasting works because (3,) is treated as (1,3) and is broadcast across each row of the (3,4) array.
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)
✗ Incorrect
Shapes (2,3) and (3,) broadcast because (3,) is treated as (1,3) and 1 can be stretched to 2.
What happens if broadcasting rules are not met?
AOperation returns None
BNumpy silently reshapes arrays
CArrays are converted to lists
DNumpy raises a ValueError
✗ Incorrect
If shapes are incompatible, numpy raises a ValueError explaining the mismatch.
Which numpy function can help check array shapes before broadcasting?
Anumpy.shape
Bnumpy.reshape
Cnumpy.broadcast_shapes
Dnumpy.concatenate
✗ Incorrect
numpy.broadcast_shapes returns the broadcasted shape or raises an error if shapes are incompatible.
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.
Practice
(1/5)
1. What does broadcasting in NumPy allow you to do with arrays of different shapes?
easy
A. Only add arrays if they have the exact same shape
B. Sort arrays in ascending order
C. Convert arrays into lists automatically
D. Perform element-wise operations without manually reshaping arrays
Solution
Step 1: Understand broadcasting concept
Broadcasting lets NumPy perform math on arrays with different shapes by stretching smaller arrays.
Step 2: Identify what broadcasting enables
This means you can do element-wise operations without manually reshaping arrays to the same shape.
Final Answer:
Perform element-wise operations without manually reshaping arrays -> Option D
Quick Check:
Broadcasting = element-wise operations on different shapes [OK]
Hint: Broadcasting stretches smaller arrays to match bigger ones [OK]
Common Mistakes:
Thinking arrays must have the same shape to operate
Confusing broadcasting with sorting or reshaping
Assuming broadcasting copies data
2. Which of the following is the correct way to add a 1D array b = np.array([1, 2, 3]) to each row of a 2D array a with shape (4, 3)?
easy
A. a + b
B. a + b.T
C. a + b.reshape(3, 1)
D. a + b.reshape(1, 4)
Solution
Step 1: Check shapes of arrays
Array a has shape (4, 3), and b has shape (3,). Adding a + b broadcasts b across rows.
Step 2: Understand broadcasting rules
Since b matches the number of columns, it adds to each row correctly without reshaping.
Final Answer:
a + b -> Option A
Quick Check:
1D array added across rows = a + b [OK]
Hint: Add 1D array directly if its length matches columns [OK]
Common Mistakes:
Trying to transpose 1D array which has no effect
Reshaping to wrong dimensions causing errors
Confusing rows and columns in shapes
3. What is the output of the following code?
import numpy as np
a = np.array([[1, 2, 3], [4, 5, 6]])
b = np.array([10, 20, 30])
result = a + b
print(result)
medium
A. [[11 22 33]
[14 25 36]]
B. [[11 12 13]
[14 15 16]]
C. [[10 20 30]
[10 20 30]]
D. Error due to shape mismatch
Solution
Step 1: Analyze shapes and operation
a shape is (2,3), b shape is (3,). Adding broadcasts b across rows.
Hint: Add 1D array to 2D rows matches columns [OK]
Common Mistakes:
Expecting error due to different shapes
Adding element-wise without broadcasting
Confusing rows and columns in addition
4. Identify the error in this code snippet:
import numpy as np
a = np.array([[1, 2], [3, 4], [5, 6]])
b = np.array([10, 20, 30])
result = a + b
print(result)
medium
A. Using wrong operator for addition
B. Syntax error in array creation
C. Shapes of a and b are incompatible for broadcasting
D. No error, code runs fine
Solution
Step 1: Check shapes of arrays
a shape is (3, 2), b shape is (3,). They differ in second dimension.
Step 2: Understand broadcasting rules
Broadcasting requires trailing dimensions to match or be 1. Here, 2 != 3, so addition fails.
Final Answer:
Shapes of a and b are incompatible for broadcasting -> Option C
Quick Check:
Shape mismatch causes broadcasting error [OK]
Hint: Check trailing dimensions match or are 1 for broadcasting [OK]
Common Mistakes:
Assuming any shapes can broadcast
Ignoring dimension order in arrays
Thinking 1D arrays always broadcast to 2D
5. You have a 2D array data of shape (5, 4) representing 5 samples with 4 features each. You want to subtract the mean of each feature (given as a 1D array mean_vals of length 4) from the corresponding feature column in data. Which code correctly applies broadcasting to do this?
hard
A. data - mean_vals.reshape(4, 1)
B. data - mean_vals
C. data - mean_vals.reshape(1, 5)
D. data - mean_vals.T
Solution
Step 1: Understand shapes and operation
data shape is (5, 4), mean_vals shape is (4,). Subtracting mean_vals from data broadcasts mean_vals across rows.
Step 2: Confirm correct broadcasting
Since mean_vals length matches columns, data - mean_vals subtracts feature-wise means correctly.
Final Answer:
data - mean_vals -> Option B
Quick Check:
Subtract 1D array from 2D rows = data - mean_vals [OK]