Challenge - 5 Problems
Broadcasting Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of adding arrays with broadcasting
What is the output of this Python code using NumPy broadcasting?
Data Analysis Python
import numpy as np arr1 = np.array([[1, 2, 3], [4, 5, 6]]) arr2 = np.array([10, 20, 30]) result = arr1 + arr2 print(result)
Attempts:
2 left
💡 Hint
Remember how NumPy broadcasts a 1D array to match the shape of a 2D array when adding.
✗ Incorrect
The 1D array [10, 20, 30] is broadcasted to each row of the 2D array arr1, so each element in arr1's rows is added to the corresponding element in arr2.
❓ data_output
intermediate2:00remaining
Shape after broadcasting in multiplication
Given these arrays, what is the shape of the result after multiplication with broadcasting?
Data Analysis Python
import numpy as np arr1 = np.ones((3, 1, 4)) arr2 = np.ones((1, 5, 1)) result = arr1 * arr2 print(result.shape)
Attempts:
2 left
💡 Hint
Broadcasting aligns dimensions from the right and expands dimensions of size 1.
✗ Incorrect
The shapes (3,1,4) and (1,5,1) broadcast to (3,5,4) by expanding the singleton dimensions.
🔧 Debug
advanced2:00remaining
Identify the broadcasting error
What error does this code raise and why?
Data Analysis Python
import numpy as np arr1 = np.array([1, 2, 3]) arr2 = np.array([[1, 2], [3, 4]]) result = arr1 + arr2 print(result)
Attempts:
2 left
💡 Hint
Check if the shapes of the arrays can be broadcasted according to the rules.
✗ Incorrect
The shapes (3,) and (2,2) cannot be broadcasted because their dimensions are incompatible from the right side.
❓ visualization
advanced2:00remaining
Visualize broadcasting with arrays
Which option correctly describes the broadcasting process when adding these arrays?
Data Analysis Python
import numpy as np arr1 = np.array([[1], [2], [3]]) # shape (3,1) arr2 = np.array([10, 20, 30]) # shape (3,) result = arr1 + arr2 print(result)
Attempts:
2 left
💡 Hint
Remember that a 1D array of shape (3,) can be treated as (1,3) for broadcasting with (3,1).
✗ Incorrect
arr2 is treated as shape (1,3) and broadcasted to (3,3). arr1 is broadcasted from (3,1) to (3,3). The result shape is (3,3).
🧠 Conceptual
expert2:00remaining
Understanding broadcasting rules with multiple dimensions
Given arrays with shapes (4,1,3,1) and (1,5,1,7), what is the resulting shape after broadcasting?
Attempts:
2 left
💡 Hint
Broadcasting compares dimensions from right to left, expanding singleton dimensions or matching equal sizes.
✗ Incorrect
Each dimension is compared: 1 vs 7 → 7, 3 vs 1 → 3, 1 vs 5 → 5, 4 vs 1 → 4. Result shape is (4,5,3,7).