Bird
0
0

The following code throws an error. What is the problem?

medium📝 Debug Q14 of 15
NumPy - Aggregation Functions
The following code throws an error. What is the problem?
import numpy as np
arr = np.array([[1, 2], [3, 4]])
cum_sum = np.cumsum(arr, axis=2)
print(cum_sum)
ASyntax error in np.cumsum call
Bnp.cumsum does not accept axis parameter
CArray must be 1D for cumsum
DAxis 2 does not exist for a 2D array
Step-by-Step Solution
Solution:
  1. Step 1: Check array dimensions

    The array has shape (2, 2), so it has axes 0 and 1 only.
  2. Step 2: Validate axis parameter

    Using axis=2 is invalid because axis 2 does not exist for this 2D array, causing an error.
  3. Final Answer:

    Axis 2 does not exist for a 2D array -> Option D
  4. Quick Check:

    Axis must be within array dimensions [OK]
Quick Trick: Axis must be less than array dimensions [OK]
Common Mistakes:
  • Assuming axis parameter is optional but invalid
  • Thinking cumsum only works on 1D arrays
  • Confusing syntax error with axis error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes