0
0
NumPydata~20 mins

Broadcasting for outer products in NumPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Broadcasting Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
A
[[ 4  5]
 [ 8 10]
 [12 15]]
B
[[ 4  8 12]
 [ 5 10 15]]
C
[[ 4  5  6]
 [ 8 10 12]
 [12 15 18]]
D
[[ 5  4]
 [10  8]
 [15 12]]
Attempts:
2 left
💡 Hint
Remember that x[:, None] reshapes x to a column vector to enable broadcasting with y.
data_output
intermediate
1: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)
A(4, 3)
B(3, 4)
C(4,)
D(3,)
Attempts:
2 left
💡 Hint
Think about how reshaping x to (4,1) and y to (3,) broadcasts to a 2D array.
🔧 Debug
advanced
1: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)
AIndexError: index out of bounds
BTypeError: unsupported operand type(s) for *: 'int' and 'list'
CNo error, output is [4 10 15]
DValueError: operands could not be broadcast together with shapes (3,) (2,)
Attempts:
2 left
💡 Hint
Check the shapes of x and y before multiplication.
🚀 Application
advanced
2: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?
Aresult = x * y[:, None]
Bresult = x[:, None] * y
Cresult = np.dot(x, y)
Dresult = np.outer(x, y) + 1
Attempts:
2 left
💡 Hint
Remember the shape changes needed for broadcasting to work as outer product.
🧠 Conceptual
expert
2: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?
Ax[:, None] transposes x, y is broadcast to match x's shape, resulting in a dot product instead of outer product.
Bx[:, None] flattens x to 1D, y is reshaped to 2D, so broadcasting sums the arrays to produce the outer product.
Cx[:, None] reshapes x to a 2D column vector, y remains 1D, so broadcasting expands y to a row vector, enabling element-wise multiplication to produce a 2D outer product.
Dx[:, None] duplicates x elements, y duplicates y elements, and their multiplication produces a scalar product.
Attempts:
2 left
💡 Hint
Focus on how reshaping changes array dimensions and how broadcasting works with those shapes.