Bird
0
0

Given two arrays:

hard📝 Application Q15 of 15
NumPy - Array Operations
Given two arrays:
a = np.array([[1, 2], [3, 4]])
b = np.array([10, 20])

What is the result of a + b and why?
A<code>[[11 22] [13 24]]</code> because <code>b</code> is broadcasted to each row of <code>a</code>
B<code>[[10 20] [10 20]]</code> because <code>b</code> replaces <code>a</code>
C<code>[[1 2] [3 4]]</code> because addition is not possible
DError due to shape mismatch
Step-by-Step Solution
Solution:
  1. Step 1: Analyze shapes of arrays

    a is shape (2,2), b is shape (2,). Numpy broadcasts b across rows of a.
  2. Step 2: Perform element-wise addition with broadcasting

    Add b to each row of a: first row [1+10, 2+20], second row [3+10, 4+20].
  3. Final Answer:

    [[11 22] [13 24]] -> Option A
  4. Quick Check:

    Broadcasting adds b to each row of a [OK]
Quick Trick: Broadcast smaller array to match larger shape for element-wise ops [OK]
Common Mistakes:
  • Expecting error due to different shapes
  • Thinking b replaces a instead of broadcasting
  • Ignoring numpy broadcasting rules

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes