Bird
0
0

What will be the output of the following code?

medium📝 Predict Output Q13 of 15
NumPy - Broadcasting
What will be the output of the following code?
import numpy as np
shape = np.broadcast_shapes((4, 1, 3), (1, 5, 3))
print(shape)
A(4, 1, 3)
B(4, 5, 3)
C(1, 5, 3)
DValueError: shapes not compatible
Step-by-Step Solution
Solution:
  1. Step 1: Compare shapes dimension-wise from right

    Rightmost dims: 3 and 3 match. Next dims: 1 and 5, 1 broadcasts to 5. Leftmost dims: 4 and 1, 1 broadcasts to 4.
  2. Step 2: Determine broadcasted shape

    Broadcasted shape is (4, 5, 3) combining max of each dimension.
  3. Final Answer:

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

    Broadcast dims max or 1 expands [OK]
Quick Trick: Broadcast dims take max or 1 expands to max [OK]
Common Mistakes:
  • Confusing which dimension broadcasts to which
  • Expecting error when shapes are compatible
  • Mixing up order of dimensions

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes