Bird
0
0

You have a 3D array A with shape (4,1,5) and a 2D array B with shape (1,3). You want to add them using broadcasting. What will be the shape of the result?

hard📝 Application Q15 of 15
NumPy - Broadcasting
You have a 3D array A with shape (4,1,5) and a 2D array B with shape (1,3). You want to add them using broadcasting. What will be the shape of the result?
ABroadcasting will fail with ValueError
B(4, 1, 3)
C(1, 3, 5)
D(4, 3, 5)
Step-by-Step Solution
Solution:
  1. Step 1: Align shapes by adding missing dimension

    A shape is (4,1,5), B shape is (1,3). Treat B as (1,1,3) to match 3D shape.
  2. Step 2: Compare dimensions right to left

    For each dim: 5 vs 3 (A's last dim 5, B's last dim 3) - mismatch. This means broadcasting fails.
  3. Step 3: Re-examine dimension matching

    Actually, B's shape is (1,3), so when adding to A (4,1,5), B is broadcast to (1,1,3). Compare dims: 5 vs 3 (A's last dim 5, B's last dim 3) - mismatch. NumPy aligns shapes from right: A (4,1,5), B (1,1,3). Last dims 5 and 3 mismatch, so broadcasting fails.
  4. Final Answer:

    Broadcasting will fail with ValueError -> Option A
  5. Quick Check:

    Mismatch in last dims 5 and 3 causes failure [OK]
Quick Trick: Align dims right to left; mismatch without 1 causes error [OK]
Common Mistakes:
  • Assuming missing dims auto-align without prepending
  • Ignoring last dimension mismatch
  • Thinking broadcasting always works if dims differ

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes