Computing the variance
It is important to have some understanding of what commonly-used functions are doing under the hood. Though you may already know how to compute variances, this is a beginner course that does not assume so. In this exercise, we will explicitly compute the variance of the petal length of Iris veriscolor using the equations discussed in the videos. We will then use np.var() to compute it.
Cet exercice fait partie du cours
Statistical Thinking in Python (Part 1)
Instructions
- Create an array called
differencesthat is the difference between the petal lengths (versicolor_petal_length) and the mean petal length. The variableversicolor_petal_lengthis already in your namespace as a NumPy array so you can take advantage of NumPy's vectorized operations. - Square each element in this array. For example,
x**2squares each element in the arrayx. Store the result asdiff_sq. - Compute the mean of the elements in
diff_squsingnp.mean(). Store the result asvariance_explicit. - Compute the variance of
versicolor_petal_lengthusingnp.var(). Store the result asvariance_np. - Print both
variance_explicitandvariance_npin oneprintcall to make sure they are consistent.
Exercice interactif pratique
Essayez cet exercice en complétant cet exemple de code.
# Array of differences to mean: differences
# Square the differences: diff_sq
# Compute the mean square difference: variance_explicit
# Compute the variance using NumPy: variance_np
# Print the results