Bird
0
0

Find the error in this code snippet:

medium📝 Debug Q6 of 15
NumPy - Aggregation Functions
Find the error in this code snippet:
import numpy as np
arr = np.array([1, 2, 3])
mean_val = np.mean(arr, axis=2)
print(mean_val)
Anp.mean() requires at least 2 arguments
BAxis 2 does not exist for 1D array
Cnp.mean() cannot be used on arrays
DMissing parentheses in print statement
Step-by-Step Solution
Solution:
  1. Step 1: Check array dimensions

    The array is 1D with shape (3,), so valid axes are 0 only.
  2. Step 2: Understand axis parameter

    Axis=2 is invalid because the array has no third dimension, causing an error.
  3. Final Answer:

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

    Axis must be within array dimensions [OK]
Quick Trick: Axis must be less than array dimensions [OK]
Common Mistakes:
  • Using invalid axis
  • Confusing axis with index
  • Syntax errors in print

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes