Bird
0
0

Which of the following is the correct syntax to add a 1D array to each row of a 2D array using broadcasting?

easy📝 Syntax Q3 of 15
NumPy - Broadcasting
Which of the following is the correct syntax to add a 1D array to each row of a 2D array using broadcasting?
Aresult = arr2d + arr1d
Bresult = arr2d + arr1d.reshape(-1, 1)
Cresult = arr2d + arr1d.T
Dresult = arr2d + arr1d.flatten()
Step-by-Step Solution
Solution:
  1. Step 1: Understand shapes of arrays

    A 2D array plus a 1D array with matching trailing dimension broadcasts correctly.
  2. Step 2: Identify correct syntax

    Adding arr1d directly to arr2d adds arr1d to each row by broadcasting.
  3. Final Answer:

    result = arr2d + arr1d -> Option A
  4. Quick Check:

    Direct addition with compatible shapes broadcasts correctly [OK]
Quick Trick: Add 1D array directly to 2D array rows if shapes align [OK]
Common Mistakes:
  • Using reshape incorrectly causing shape mismatch
  • Transposing 1D arrays which has no effect
  • Flattening arrays unnecessarily

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes