Bird
0
0

Why does this code raise an error?

medium📝 Debug Q7 of 15
NumPy - Aggregation Functions
Why does this code raise an error?
arr = np.array([[1, 2, 3], [4, 5, 6]])
result = np.min(arr, axis=-4)
Anp.min does not support negative axis values
BNegative axis index is out of range for the array dimensions
CThe array must be 3D to use negative axis indices
Dnp.min requires axis to be a positive integer
Step-by-Step Solution
Solution:
  1. Step 1: Determine array dimensions

    The array arr has shape (2, 3), so ndim=2.
  2. Step 2: Understand negative axis indexing

    Negative axis indices count from the last dimension backwards, valid range is -2 to -1.
  3. Step 3: Check axis value

    Axis -4 is less than -2, so it is out of bounds.
  4. Final Answer:

    Negative axis index is out of range for the array dimensions -> Option B
  5. Quick Check:

    ndim=2, axis=-4 invalid [OK]
Quick Trick: Negative axis must be >= -ndim [OK]
Common Mistakes:
  • Believing negative axis can be any negative number
  • Confusing axis indexing rules
  • Assuming np.min only accepts positive axes

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes