Bird
0
0

Given a 2D array arr of shape (4, 5), you want to compute the standard deviation along rows but ignore NaN values. Which NumPy function and axis parameter combination is correct?

hard📝 Application Q9 of 15
NumPy - Aggregation Functions
Given a 2D array arr of shape (4, 5), you want to compute the standard deviation along rows but ignore NaN values. Which NumPy function and axis parameter combination is correct?
Anp.nanstd(arr, axis=1)
Bnp.std(arr, axis=0)
Cnp.nanstd(arr, axis=0)
Dnp.std(arr, axis=1)
Step-by-Step Solution
Solution:
  1. Step 1: Identify function to ignore NaNs

    np.nanstd computes standard deviation ignoring NaN values.
  2. Step 2: Choose axis for row-wise std

    Axis=1 computes along columns for each row, so std per row.
  3. Final Answer:

    np.nanstd(arr, axis=1) correctly computes row-wise std ignoring NaNs -> Option A
  4. Quick Check:

    Ignore NaN and std row-wise = np.nanstd + axis=1 [OK]
Quick Trick: Use np.nanstd to ignore NaNs, axis=1 for rows [OK]
Common Mistakes:
  • Using np.std which includes NaNs
  • Choosing wrong axis for rows
  • Confusing axis=0 and axis=1

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes