Bird
0
0

What is the output of the following code?

medium📝 Predict Output Q13 of 15
NumPy - Aggregation Functions
What is the output of the following code?
import numpy as np
arr = np.array([2, 4, 4, 4, 5, 5, 7, 9])
print(np.var(arr))
A2.0
B4.0
C2.5
D3.0
Step-by-Step Solution
Solution:
  1. Step 1: Calculate the mean of the array

    The array is [2,4,4,4,5,5,7,9]. Sum = 40, count = 8, mean = 40/8 = 5.
  2. Step 2: Calculate variance

    Variance = average of squared differences from mean:
    (2-5)^2=9, (4-5)^2=1, (4-5)^2=1, (4-5)^2=1, (5-5)^2=0, (5-5)^2=0, (7-5)^2=4, (9-5)^2=16
    Sum = 9+1+1+1+0+0+4+16 = 32
    Variance = 32/8 = 4.0
  3. Step 3: Check code output

    NumPy's np.var() by default calculates population variance (divides by N). So output is 4.0.
  4. Final Answer:

    4.0 -> Option B
  5. Quick Check:

    Variance = 4.0 [OK]
Quick Trick: Variance = mean of squared differences [OK]
Common Mistakes:
  • Calculating sample variance (dividing by N-1) instead of population
  • Mixing variance with standard deviation
  • Incorrect mean calculation

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes