Bird
0
0

What will be the shape of the result after broadcasting in this code?

medium📝 Predict Output Q5 of 15
NumPy - Broadcasting
What will be the shape of the result after broadcasting in this code?
import numpy as np
X = np.ones((4, 1, 3))
Y = np.ones((1, 5, 1))
Z = X + Y
print(Z.shape)
A(4, 5, 1)
B(4, 1, 1)
C(1, 5, 3)
D(4, 5, 3)
Step-by-Step Solution
Solution:
  1. Step 1: Compare shapes dimension-wise

    X shape: (4,1,3), Y shape: (1,5,1). Broadcasting rules apply per dimension.
  2. Step 2: Determine broadcasted shape

    Dimensions: 4 vs 1 -> 4, 1 vs 5 -> 5, 3 vs 1 -> 3. Result shape is (4,5,3).
  3. Final Answer:

    (4, 5, 3) -> Option D
  4. Quick Check:

    Broadcast shape = max dims or 1 [OK]
Quick Trick: Broadcast shape is max dimension size or 1 per axis [OK]
Common Mistakes:
  • Taking minimum instead of maximum dimension
  • Ignoring dimension order
  • Assuming shapes must be equal

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes