Bird
0
0

What is wrong with this code snippet?

medium📝 Debug Q14 of 15
NumPy - Aggregation Functions
What is wrong with this code snippet?
arr = np.array([[1, 2], [3, 4]])
result = np.mean(arr, axis=2)
ANo error, code runs fine
Bnp.mean cannot be used on arrays
CMissing parentheses in np.array
Daxis=2 is invalid for 2D array
Step-by-Step Solution
Solution:
  1. Step 1: Check array dimensions

    The array arr is 2D with shape (2,2), so valid axes are 0 and 1.
  2. Step 2: Validate axis parameter

    Using axis=2 is invalid and causes an error because axis 2 does not exist.
  3. Final Answer:

    axis=2 is invalid for 2D array -> Option D
  4. Quick Check:

    Axis must be less than array dimensions [OK]
Quick Trick: Axis must be less than array dimensions [OK]
Common Mistakes:
  • Using axis number too large for array
  • Thinking np.mean can't be used on arrays
  • Ignoring error messages

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes