Bird
0
0

Why does np.sum(arr, axis=1) raise an error if arr is a 1D array?

medium📝 Debug Q7 of 15
NumPy - Aggregation Functions
Why does np.sum(arr, axis=1) raise an error if arr is a 1D array?
ABecause axis must be a negative integer for 1D arrays.
BBecause np.sum does not support axis parameter for 1D arrays.
CBecause arr must be 2D to use np.sum.
DBecause axis=1 is out of bounds for 1D arrays which only have axis 0.
Step-by-Step Solution
Solution:
  1. Step 1: Check dimensions of 1D array

    A 1D array has only one axis: axis 0.
  2. Step 2: Validate axis parameter

    axis=1 is invalid for 1D arrays and causes an IndexError.
  3. Final Answer:

    Because axis=1 is out of bounds for 1D arrays which only have axis 0. -> Option D
  4. Quick Check:

    1D arrays have only axis 0 [OK]
Quick Trick: 1D arrays only have axis 0 [OK]
Common Mistakes:
  • Using axis=1 on 1D arrays
  • Thinking axis parameter is optional
  • Confusing array dimensions

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes