In this short tutorial, I will show you how to calculate variance in Python
with numpy
, statistics
modules and with built-in functions.
Doing statistics is fun for me so I hope you will find this exercise enjoyable as well. Before jumping into the calculation, let’s refresh our memory with Variance definition.
What is Variance?
Variance in statistics is a measurement of the spread between numbers in a data set. Here is an example to explain it.
Everyone knows that Curry led the NBA in free-throw percentage with 90.31%. His free-throw is very consistent so I expect the variance to be small and close to the mean. As for me, some day I make 10 free-throws in a row. Another day, I make none. So I am very inconsistent so the variance is very large.
In other words, without knowing Curry free-throw skills, you can tell that he has an amazing basketball free-throw skills by just looking at the variance.
Calculate Variance of a List in Python
Read below examples to see how to find variance in python in different ways.
Using numpy module
The first and most popular way to calculate variance is to use numpy because it’s the fundamental package needed for scientific computing with Python. You will find calculating variance super easy! Look at the code block below.
|
|
Using built-in functions
Sometimes, you cannot install any external python module or package to calculate variance. So you must rely on built in function. In example below, you will learn how to use variance formula in Python
|
|
Using statistics module
If you are using Python 3.4
or later, you can use variance function from statistics module that comes with Python installation by default.
|
|
Conclusion
And this is how to compute the variance of a data set in Python with 3 different ways including the numpy module. I hope you find this short tutorial useful. If you know any other way to calculate the variance in Python, please share in the comment section below. Have a great day!