Python

Python standard deviation tutorial

The standard deviation allows you to measure how spread out numbers in a data set are. Large values of standard deviations show that elements in a data set are spread further apart from their mean value. In this tutorial, we will calculate the standard deviation using Python

Small standard deviations show that items don’t deviate significantly from the mean value of a data set.


 

 

Terminology

There are two standard deviation notions in statistics.

One is the population standard deviation. It computes the spread directly from all values in a population. You use it when the values you have at hand represent the entire population.

Another one is the sample standard deviation. It tries to estimate the population spread by using only a sample subset of values. You use it when the values you have at hand represent just a subset of the entire population.

The sample standard deviation is an approximate measure. It’s useful because frequently, the data population is too big, and all we can only directly measure a randomized sample of it.

Population and sample standard deviations are calculated using slightly different algorithms. Hence, when programming, you should always keep in mind which one you want to compute and call the appropriate APIs.


Standard deviation in Python

Since version 3.x Python includes a light-weight statistics module in a default distribution. This module provides a lot of useful functions for statistical computations.

There is also a full-featured statistics package NumPy, which is especially popular among data scientists.

The latter has more features but also represents a more massive dependency in your code.


Calculate for a list

Computing sample standard deviation on a list of values in Python can be accomplished with the statistics.stdev() function.

stdev list


Population standard deviation is computed using slightly different function statistics.pstdev().

pstdev list


In the examples which follow, we’ll be showing how to apply statistics.stdev() function to different Python data types. If you need to calculate the population standard deviation, use statistics.pstdev() function instead. The rest of the code must be identical.

Another option to compute a standard deviation for a list of values in Python is to use a NumPy scientific package.

It doesn’t come with Python by default, and you need to install it separately. The usual way of installing third-party packages in Python is to use a Python package installer pip.

pip numpy


After you installed NumPy, computing the standard deviation is trivial. Note, that numpy.std computes population standard deviation by default.

numpy std


If you want to compute a sample standard deviation using the NumPy package, you will have to pass an additional argument ddof with a value of 1. ddof stands for delta degrees of freedom, which is a statistical notion used in estimating statistics of populations from samples of them.

numpy std ddof


 


Calculate for an array

If you work with large data sets, Python arrays may be more convenient than its more popular lists.

You can also perform an arithmetic operation on array variables as if they were singular values. In that case, an arithmetic operation will be applied to each value in an array independently.

In the example below, we also pass a d argument to an array constructor to indicate that our values are of type double.

stdev array


numpy.std works on array values as well.

stdev numpy array


 

Calculate for dictionary values

Sometimes your data is stored in a key-value data structure like the Python dict, rather than a sequential data structure like a list.

For example, you can have a data structure, which maps students to their test scores, as in the picture below.

If you want to compute a standard deviation of the test scores across all students, you can do it by calling statistics.pstdev on the dictionary values, without the keys. For that, call Python’s built-in dict.values() function.

dict stdev


 

Calculate for a matrix

For dealing with matrices, it is best to resort to the NumPy package. NumPy provides a numpy.matrix data type specifically designed for working with matrices.

Let’s generate a square 4×4 matrix.


With matrices, there are three ways to compute standard deviations.

You can compute standard deviations by column (numpy.matrix.std(0)), by row (numpy.matrix.std(1)) or for all elements, as if the matrix was a vector (numpy.matrix.std()).

matrix std


 


Calculate for Pandas Series

pandas.Series is a one-dimensional array with axis labels. It builds on top of numpy.ndarray.

One of its applications is for working with time-series data.

Calculating the sample standard deviation from pandas.Series is easy.

pandas data series sample


If you need to calculate the population standard deviation, just pass in an additional ddof argument like below.

pandas data series population


 

Calculate for Pandas DataFrame

pandas.DataFrame is a two-dimensional tabular data structure, which allows to easily perform arithmetic operations on both rows and columns.

Its closest analogy in pure Python is the dict data type.

Let’s create a DataFrame object that represents students’ test scores, as we did in the dict example above.

dataframe


Now we can calculate sample standard deviations for each subject, namely Math and History. Note that it will be by-row calculations.

dataframe std by row


Alternatively, we can compute sample standard deviations by person. For that, we’ll pass an additional axis argument with a value equal to 1. Note, that in this case, it will be by-column calculations.

dataframe std by column


From the picture above, you can see that Alex has the highest standard deviation of 30.4. It makes sense, because the spread in his scores is much bigger compared to Kate and Cindy.

All of the above were sample standard deviations. To compute a population standard deviation, pass an additional ddof argument with a value equal to 0 as usual.

dataframe std population


In the following three sections we will focus on telling the differences between standard deviation and other statistical aggregate measures such as mean, average and median.


Standard deviation vs. mean (average)

As mentioned above, the standard deviation is a measure of how spread out numbers in a data set are. Another interpretation of standard deviation is how far each element in a data set is from the mean value of this data set.

What is the mean? The mean number is just an abstract concept that tries to estimate an average value in a data set. It’s obtained by summing up all numbers in a data set and dividing the result by the quantity of these numbers (i.e., the size of the data set).

Below is an example of how you would obtain a mean number for a data set. You can also see that the standard deviation value for this data set is quite different from its mean value.

mean number


 

Standard deviation vs. median

Median is another aggregate measure in statistics. It is meant to express the notion of an average number. However, it’s different from the mean number.

Imagine that you have a data set, and you arranged all numbers in this data set in a non-decreasing order. For example [1, 2, 4, 5, 6].

You can see that 4 falls right into the middle of this sorted data set. Such number, which stands in the middle of a data set after we have arranged it in non-decreasing order, is called the median value of this data set.

If the size of the data set is even, as in [1, 2, 4, 5, 6, 7], you will end up having two numbers in the middle, in this case, 4 and 5. In such a case, you compute the median value as the mean value of these two numbers, i.e., 4.5 in this example.

Below is an example of calculating the median value for a data set. Note that it’s again quite different from the standard deviation.

median


 

Pooled standard deviation

Sometimes, when you have multiple samples of your data, you will want to estimate the standard deviation of your population using all of those sample standard deviations. This is the scenario when the pooled standard deviation comes handy.

The pooled standard deviation is just a weighted average of all of your sample standard deviations. The more items there are in a sample, the more weight you give to this sample’s standard deviation in the computation of the pooled standard deviation.

Below is an example of how one can compute the pooled standard deviation.

pooled standard deviation


 

Plot standard deviation and error bars

If you want to plot statistical data in Python, you can use the matplotlib 2D plotting library.

You install matplotlib using pip3 install matplotlib command in your terminal.

Install matplotlib


Let’s create a plot of mean values of students’ scores by subject and use standard deviation for showing our confidence ranges (also known as error bars).

Let’s prepare the data as in the example below. Compute means and standard deviations of scores by subject.

Prepare data


Then plug x, y and yerr as inputs to matplotlib.pyplot.errorbar() function. matplotlib.pyplot.show() will then display a nice error bar chart.

Plot terminal


Plot GUI

I hope you find the tutorial useful. Keep coming back.

 

Mokhtar Ebrahim
I'm working as a Linux system administrator since 2010. I'm responsible for maintaining, securing, and troubleshooting Linux servers for multiple clients around the world. I love writing shell and Python scripts to automate my work.

Leave a Reply

Your email address will not be published. Required fields are marked *