Bird
0
0

Identify the error in this code snippet:

medium📝 Debug Q7 of 15
NumPy - Aggregation Functions
Identify the error in this code snippet:
import numpy as np
arr = np.array([[1, 2], [3, 4]])
cum_sum = np.cumsum(arr, axis=3)
print(cum_sum)
AAxis 3 is out of bounds for a 2D array
Bnp.cumsum() cannot be used on 2D arrays
CThe array must be flattened before applying np.cumsum()
DThe code is correct and will run without errors
Step-by-Step Solution
Solution:
  1. Step 1: Check array dimensions

    The array arr is 2D with shape (2, 2).
  2. Step 2: Validate axis parameter

    Axis=3 refers to the 4th dimension, which does not exist in a 2D array.
  3. Step 3: Resulting error

    This causes an 'axis out of bounds' error.
  4. Final Answer:

    Axis 3 is out of bounds for a 2D array -> Option A
  5. Quick Check:

    Confirm array ndim and axis value compatibility [OK]
Quick Trick: Axis must be less than array dimensions [OK]
Common Mistakes:
  • Using axis value exceeding array dimensions
  • Assuming np.cumsum requires flattening
  • Ignoring error messages about axis

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes