Challenge - 5 Problems
Broadcasting Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of broadcasting outer product with 1D arrays
What is the output of this code snippet using NumPy broadcasting to compute an outer product?
NumPy
import numpy as np x = np.array([1, 2, 3]) y = np.array([4, 5]) result = x[:, None] * y print(result)
Attempts:
2 left
💡 Hint
Remember that x[:, None] reshapes x to a column vector to enable broadcasting with y.
✗ Incorrect
The expression x[:, None] reshapes x from shape (3,) to (3,1). Multiplying by y with shape (2,) broadcasts y to (1,2), resulting in a (3,2) array where each element is the product of elements from x and y.
❓ data_output
intermediate1:30remaining
Shape of result from broadcasting outer product
Given two arrays x with shape (4,) and y with shape (3,), what is the shape of the result when computing x[:, None] * y using NumPy broadcasting?
NumPy
import numpy as np x = np.arange(4) y = np.arange(3) result = x[:, None] * y print(result.shape)
Attempts:
2 left
💡 Hint
Think about how reshaping x to (4,1) and y to (3,) broadcasts to a 2D array.
✗ Incorrect
x[:, None] reshapes x to (4,1). y has shape (3,). Broadcasting y to (1,3) and multiplying results in shape (4,3).
🔧 Debug
advanced1:30remaining
Identify the error in broadcasting outer product code
What error does this code raise when trying to compute an outer product using broadcasting?
NumPy
import numpy as np x = np.array([1, 2, 3]) y = np.array([4, 5]) result = x * y print(result)
Attempts:
2 left
💡 Hint
Check the shapes of x and y before multiplication.
✗ Incorrect
x has shape (3,) and y has shape (2,). They cannot be broadcast together directly for element-wise multiplication, causing a ValueError.
🚀 Application
advanced2:00remaining
Use broadcasting to compute outer product without loops
Which code snippet correctly computes the outer product of two 1D NumPy arrays x and y without using loops?
Attempts:
2 left
💡 Hint
Remember the shape changes needed for broadcasting to work as outer product.
✗ Incorrect
x[:, None] reshapes x to a column vector, y stays as a row vector, so multiplication broadcasts to outer product. np.dot computes inner product, np.outer adds 1 changes values.
🧠 Conceptual
expert2:30remaining
Understanding broadcasting rules for outer products
Which statement best explains why x[:, None] * y computes the outer product of 1D arrays x and y in NumPy?
Attempts:
2 left
💡 Hint
Focus on how reshaping changes array dimensions and how broadcasting works with those shapes.
✗ Incorrect
Reshaping x to (n,1) and y to (m,) allows broadcasting to (n,m) for element-wise multiplication, which is the definition of outer product.