Bird
0
0

What will be the output of the following code?

medium📝 Predict Output Q5 of 15
NumPy - Broadcasting
What will be the output of the following code?
import numpy as np
shape = np.broadcast_shapes((1, 4), (3, 1))
print(shape)
A(4, 3)
B(1, 4, 3, 1)
C(3, 4)
DError: shapes are not compatible
Step-by-Step Solution
Solution:
  1. Step 1: Analyze shapes

    Shapes are (1, 4) and (3, 1). Align from right: 4 vs 1 and 1 vs 3.
  2. Step 2: Apply broadcasting rules

    Dimensions are compatible if equal or one is 1. Here, 4 and 1 compatible, 1 and 3 compatible.
  3. Final Answer:

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

    Check dimension-wise compatibility [OK]
Quick Trick: Broadcast dims must be equal or one [OK]
Common Mistakes:
  • Assuming shapes concatenate instead of broadcasting
  • Ignoring dimension order when aligning shapes
  • Thinking broadcasting requires same number of dimensions

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes