Bird
0
0

Identify the error in this code snippet:

medium📝 Debug Q6 of 15
NumPy - Aggregation Functions
Identify the error in this code snippet:
import numpy as np
arr = np.array([1, 2, 3])
cum_sum = np.cumsum(arr, axis=1)
print(cum_sum)
AAxis 1 is invalid for 1D array
Bnp.cumsum() requires two arguments
Carr should be a list, not numpy array
DMissing parentheses in print statement
Step-by-Step Solution
Solution:
  1. Step 1: Check array dimensions

    arr is 1D with shape (3,). Axis 1 does not exist.
  2. Step 2: Understand axis parameter validity

    Axis must be 0 or None for 1D arrays; axis=1 causes an error.
  3. Final Answer:

    Axis 1 is invalid for 1D array -> Option A
  4. Quick Check:

    Axis must be valid for array dimension [OK]
Quick Trick: Axis must be less than array dimensions [OK]
Common Mistakes:
  • Using axis=1 on 1D array
  • Thinking np.cumsum needs two args
  • Confusing print syntax errors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes