Bird
0
0

The following code throws an error. What is the cause?

medium📝 Debug Q14 of 15
NumPy - Broadcasting
The following code throws an error. What is the cause?
import numpy as np
np.broadcast_shapes((3, 2), (2, 3))
AShapes are not compatible for broadcasting
BFunction broadcast_shapes does not exist
CTuples must be of same length
DMissing import statement
Step-by-Step Solution
Solution:
  1. Step 1: Check broadcasting rules for shapes (3, 2) and (2, 3)

    Compare from right: 2 vs 3 are different and neither is 1, so incompatible.
  2. Step 2: Confirm error cause

    Since shapes cannot broadcast, numpy.broadcast_shapes raises ValueError.
  3. Final Answer:

    Shapes are not compatible for broadcasting -> Option A
  4. Quick Check:

    Broadcast dims must be equal or 1 [OK]
Quick Trick: Check dims right to left; mismatch without 1 causes error [OK]
Common Mistakes:
  • Thinking function is missing or typo
  • Assuming tuples must be same length
  • Ignoring broadcasting dimension rules

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes