Bird
0
0

The following code throws an error. What is the problem?

medium📝 Debug Q14 of 15
NumPy - Aggregation Functions
The following code throws an error. What is the problem?
import numpy as np
arr = [1, 2, 3, 4]
result = np.mean(arr, axis=1)
print(result)
Anp.mean() cannot be used on lists
BThe list is 1D, so axis=1 is invalid
CMissing parentheses in np.mean
DThe variable name 'arr' is reserved
Step-by-Step Solution
Solution:
  1. Step 1: Check the array shape

    The variable arr is a 1D list with shape (4,). Axis 1 does not exist in 1D data.
  2. Step 2: Understand axis parameter

    Axis=1 means operate along columns in 2D or higher arrays. Using axis=1 on 1D data causes an error.
  3. Final Answer:

    The list is 1D, so axis=1 is invalid -> Option B
  4. Quick Check:

    Axis must exist in array shape [OK]
Quick Trick: Check array dimensions before using axis [OK]
Common Mistakes:
  • Using axis parameter on 1D arrays
  • Thinking np.mean can't handle lists
  • Confusing axis indexing

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes