Bird
0
0

Which of the following NumPy operations will cause a broadcasting error?

easy📝 Syntax Q12 of 15
NumPy - Broadcasting
Which of the following NumPy operations will cause a broadcasting error?
Anp.array([1, 2, 3]) + np.array([[1, 2], [3, 4]])
Bnp.array([1, 2, 3]) + np.array([4, 5, 6])
Cnp.array([1, 2, 3]) + np.array([[1], [2], [3]])
Dnp.array([[1], [2], [3]]) + np.array([4])
Step-by-Step Solution
Solution:
  1. Step 1: Check shapes of arrays in each option

    np.array([1, 2, 3]) + np.array([[1, 2], [3, 4]]) adds shape (3,) and (2,2), which are incompatible for broadcasting.
  2. Step 2: Confirm broadcasting compatibility

    Other options have compatible shapes: (3,) and (3,1), (3,) and (3,), (3,1) and (1,).
  3. Final Answer:

    np.array([1, 2, 3]) + np.array([[1, 2], [3, 4]]) -> Option A
  4. Quick Check:

    Shape mismatch causes error [OK]
Quick Trick: Compare array shapes dimension-wise from right [OK]
Common Mistakes:
  • Ignoring that (3,) and (2,2) cannot broadcast
  • Confusing 1D and 2D shapes
  • Assuming all arrays add without error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes