0
0
NumPydata~20 mins

Broadcasting errors and debugging in NumPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Broadcasting Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this NumPy broadcasting code?
Consider the following code snippet using NumPy arrays. What will be the output?
NumPy
import numpy as np

arr1 = np.array([[1, 2, 3], [4, 5, 6]])
arr2 = np.array([1, 0, 1])
result = arr1 + arr2
print(result)
A
[[2 2 4]
 [5 5 7]]
BValueError: operands could not be broadcast together with shapes (2,3) (3,)
C
[[2 3 4]
 [5 6 7]]
D
[[1 2 3]
 [4 5 6]]
Attempts:
2 left
💡 Hint
Remember how NumPy broadcasts arrays with compatible shapes by stretching dimensions.
Predict Output
intermediate
2:00remaining
What error does this broadcasting code raise?
What error will this code produce when run?
NumPy
import numpy as np

arr1 = np.array([[1, 2], [3, 4]])
arr2 = np.array([1, 2, 3])
result = arr1 + arr2
print(result)
AValueError: operands could not be broadcast together with shapes (2,2) (3,)
BTypeError: unsupported operand type(s) for +: 'int' and 'list'
CIndexError: index 3 is out of bounds for axis 0 with size 2
D
[[2 4 4]
 [4 6 7]]
Attempts:
2 left
💡 Hint
Check if the shapes of the arrays can be broadcast together.
🔧 Debug
advanced
2:30remaining
Identify the broadcasting bug in this code
This code tries to add two arrays but raises an error. Which line causes the broadcasting error?
NumPy
import numpy as np

arr1 = np.array([[1, 2, 3], [4, 5, 6]])
arr2 = np.array([[1], [2], [3]])
result = arr1 + arr2
print(result)
ALine 5: addition operation arr1 + arr2 causes error
BLine 3: arr1 definition shape (2,3) incompatible with arr2 (3,1)
CNo error, code runs fine
DLine 4: arr2 definition shape (3,1) incompatible with arr1 (2,3)
Attempts:
2 left
💡 Hint
Check the shapes of arr1 and arr2 before addition.
data_output
advanced
2:00remaining
What is the shape of the result after broadcasting?
Given these arrays, what is the shape of the result after adding arr1 and arr2?
NumPy
import numpy as np

arr1 = np.ones((4,1,3))
arr2 = np.ones((1,5,1))
result = arr1 + arr2
print(result.shape)
A(1, 5, 3)
B(4, 1, 5, 3)
CValueError: operands could not be broadcast together
D(4, 5, 3)
Attempts:
2 left
💡 Hint
Broadcasting works by matching dimensions from the right and stretching size 1 dimensions.
🚀 Application
expert
3:00remaining
Fix the broadcasting error to compute weighted sums
You want to compute weighted sums of rows in a 2D array using a 1D weights array. The code below raises an error. Which option fixes it correctly?
NumPy
import numpy as np

values = np.array([[10, 20, 30], [40, 50, 60]])
weights = np.array([0.1, 0.2])
weighted_sum = (values * weights).sum(axis=1)
print(weighted_sum)
Aweights = weights[:, np.newaxis]
Bweights = weights[np.newaxis, :]
Cweights = weights.reshape(3, 1)
Dweights = weights.flatten()
Attempts:
2 left
💡 Hint
Make weights shape compatible with values shape (2,3) for element-wise multiplication.