Bird
0
0

Given two numpy arrays a and b of the same shape, which code snippet correctly performs an in-place element-wise subtraction of b from a?

hard📝 Application Q9 of 15
NumPy - Array Operations
Given two numpy arrays a and b of the same shape, which code snippet correctly performs an in-place element-wise subtraction of b from a?
Aa -= b
Bnp.subtract(a, b)
Ca = a - b
Da.subtract(b)
Step-by-Step Solution
Solution:
  1. Step 1: Understand in-place subtraction syntax

    The operator '-=' subtracts elements of b from a in-place.
  2. Step 2: Check other options

    a = a - b creates new array; np.subtract(a, b) returns new array without modifying a; a.subtract(b) is invalid.
  3. Final Answer:

    a -= b -> Option A
  4. Quick Check:

    Use '-=' for in-place subtraction [OK]
Quick Trick: Use '-=' to subtract arrays in-place [OK]
Common Mistakes:
  • Using '=' creates new array
  • Assuming np.subtract modifies in-place
  • Calling non-existent methods

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes