Let’s see how to calculate variance in Numpy python module.
Variance calculations
It is very easy to calculate variance in Python. With Numpy it is even easier.
There is dedicated function in Numpy module to calculate variance.
import numpy as np my_array = np.array([1, 5, 7, 5, 43, 43, 8, 43, 6]) variance = np.var(my_array) print("Variance equals: " + str(round(variance, 2)))
How to calculate population variance and sample variance
The NumPy var() function can be used to calculate both population variance and sample variance. The population variance is calculated using all of the data points in the population, while the sample variance is calculated using a subset of the data points (the sample).
Check also:
how to calculate a Variance in Excel