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, 4])
std_dev = np.std(arr, axis=1)
print(std_dev)
Aaxis=1 is invalid for 1D array
Bnp.std() requires axis=0 for 1D arrays
Cnp.std() cannot be used on numpy arrays
DMissing parentheses in np.std
Step-by-Step Solution
Solution:
  1. Step 1: Understand array shape

    arr is 1D with shape (4,), so axis=1 does not exist.
  2. Step 2: Check axis parameter validity

    axis=1 is invalid for 1D arrays; axis=0 or None should be used.
  3. Final Answer:

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

    1D arrays have axis 0 only [OK]
Quick Trick: Use axis=0 or omit axis for 1D arrays [OK]
Common Mistakes:
  • Using invalid axis for 1D array
  • Confusing axis parameter
  • Thinking np.std can't handle 1D arrays

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes