Bird
0
0

Which of the following numpy operations correctly uses broadcasting?

easy📝 Syntax Q12 of 15
NumPy - Broadcasting
Which of the following numpy operations correctly uses broadcasting?
A<code>np.array([1, 2, 3]) + np.array([4, 5])</code>
B<code>np.array([[1], [2], [3]]) + np.array([4, 5, 6])</code>
C<code>np.array([1, 2, 3]) + 5</code>
D<code>np.array([1, 2, 3]) + np.array([[4], [5]])</code>
Step-by-Step Solution
Solution:
  1. Step 1: Check each operation for broadcasting rules

    np.array([1, 2, 3]) + np.array([4, 5]) fails because shapes (3,) and (2,) are incompatible. np.array([[1], [2], [3]]) + np.array([4, 5, 6]) works because shapes (3,1) and (3,) broadcast to (3,3). np.array([1, 2, 3]) + 5 works because scalar 5 broadcasts to (3,). np.array([1, 2, 3]) + np.array([[4], [5]]) fails due to incompatible shapes (3,) and (2,1).
  2. Step 2: Identify correct broadcasting usage

    Both np.array([[1], [2], [3]]) + np.array([4, 5, 6]) and np.array([1, 2, 3]) + 5 are valid broadcasting operations, but since only one option can be correct, and np.array([[1], [2], [3]]) + np.array([4, 5, 6]) is valid, the question needs correction to avoid multiple correct answers.
  3. Final Answer:

    np.array([1, 2, 3]) + 5 -> Option C
  4. Quick Check:

    Scalar + array always broadcasts [OK]
Quick Trick: Adding scalar to array always broadcasts correctly [OK]
Common Mistakes:
  • Assuming arrays of different lengths always broadcast
  • Not recognizing scalar broadcasting
  • Confusing shape compatibility rules

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes