In this post, we will learn how to calculate standard deviation in Python. Standard deviation is the measure of dispersion of a set of data from its mean.
There are two ways to calculate standard deviation in Python.
- Using stdev or pstdev functions of statistics package.
- Using std function of numpy package.
stdev is used when the data is just a sample of the entire population. pstdev is used when the data represents the entire population. Note that statistics is a lightweight module added in Python 3.X .
The process of finding standard deviation requires you to know whether the data you have is the entire population or it is a sample of a group.
python standard deviation example using statistics module
suppose i have 20 rose bushes in my garden and the number of roses on each bush are as follows.
9, 2, 5, 4, 12, 7, 8, 11, 9, 3, 7, 4, 12, 5, 4, 10, 9, 6, 9, 4
In such scenario, you need to use pstdev function to calculate standard deviation of this data.
Sometimes the data we have may be only a sample of the entire population.
For example , instead of considering all the rose bushes, if i consider only the first 6 bushes, then the number of roses on each bush will be
9, 2, 5, 4, 12, 7
In such cases, you need to use stdev function to calculate standard deviation of this data.
When we used the whole population, we got a standard deviation of 2.98. But when used a sample, we got a standard deviation of 3.61.
python standard deviation example using numpy
We can execute numpy.std() to calculate standard deviation.
numpy uses population standard deviation by default, which is similar to pstdev of statistics module.
If you want to use it to calculate sample standard deviation, use an additional parameter, called ddof and set it to 1.
By default ddof is 0.
Let’s now calculate standard deviation using numpy.std for a sample of the data.
This result will be same as what we got when we used stdev of statistics module.
That’s it for now on the different ways to calculate standard deviation in Python.Feel free to share this article, in case you like it 🙂