Bird
0
0

You have a 3D array with shape (4, 1, 3) and a 2D array with shape (1, 5). How can you add them without broadcasting error?

hard📝 Application Q8 of 15
NumPy - Broadcasting
You have a 3D array with shape (4, 1, 3) and a 2D array with shape (1, 5). How can you add them without broadcasting error?
AReshape the 2D array to (5, 1) and then add
BReshape the 3D array to (4, 5, 3) and then add
CAdd them directly without reshaping
DReshape the 2D array to (1, 1, 5) and then add
Step-by-Step Solution
Solution:
  1. Step 1: Analyze shapes for broadcasting

    3D array shape is (4,1,3), 2D array shape is (1,5). They differ in last two dims.
  2. Step 2: Reshape 2D array to 3D with shape (1,1,5)

    This allows broadcasting along all dimensions.
  3. Final Answer:

    Reshape the 2D array to (1, 1, 5) and then add -> Option D
  4. Quick Check:

    Match dimensions by adding singleton dims for broadcasting [OK]
Quick Trick: Add singleton dimensions to align shapes for broadcasting [OK]
Common Mistakes:
  • Reshaping arrays incorrectly causing errors
  • Trying to add without reshaping
  • Misunderstanding dimension order

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes