Bird
0
0

What is the shape of the result when adding arrays a = np.ones((3,1)) and b = np.ones((1,4)) using broadcasting?

medium📝 Predict Output Q13 of 15
NumPy - Broadcasting
What is the shape of the result when adding arrays a = np.ones((3,1)) and b = np.ones((1,4)) using broadcasting?
A(3, 1)
B(4, 3)
C(1, 4)
D(3, 4)
Step-by-Step Solution
Solution:
  1. Step 1: Analyze shapes of a and b

    a has shape (3,1), b has shape (1,4). Broadcasting compares from right: 1 vs 4 and 3 vs 1.
  2. Step 2: Apply broadcasting rules

    Dimensions 1 and 4 broadcast to 4; 3 and 1 broadcast to 3. Result shape is (3,4).
  3. Final Answer:

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

    Broadcast (3,1) + (1,4) = (3,4) [OK]
Quick Trick: Match dims right to left, stretch 1 to other size [OK]
Common Mistakes:
  • Confusing order of dimensions
  • Forgetting to broadcast size 1 dimension
  • Swapping dimensions in result shape

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes