How to Compute the Standard Deviation in Python using Numpy 


               


How to Compute the Standard Deviation in Python using Numpy



Python


In this article, we show how to compute the standard deviation in Python.

To compute the standard deviation, we use the numpy module.

The standard deviation, many times represented by σ or s, is a measure of how spread out numbers are. It is measure that is used to quantify the amount of variation or dispersion there is in a data set. A low standard deviation indicates that the data points tend to be close to the mean of the data set, while a high standard deviation indicates that the data points are spread out over a wider range of values.

In Python, we can calculate the standard deviation using the numpy module.

With numpy, the std() function calculates the standard deviation for a given data set.

In the code below, we show how to calculate the standard deviation for a data set.





So let's break down this code.

We import the numpy module as np. This means that we reference the numpy module with the keyword, np.

We then create a variable, dataset, which is equal to, [2,6,8,12,18,24,28,32]

We then get the standard deviation of this data set by using the np.std() function. So instead of this np.std() function, we specify the variable, dataset.

We then print out the standard deviation, which in this case is 10.268276389.

So let's go over the formula for standard deviation to see if this value calculated is correct.

So the formula for standard deviation is, s= √(x-x)2/n=

So this means that in order to calculate the standard deviation, we must first calculate the mean of the data set. The mean in this case is, (2+6+8+12+18+24+28+32)/8= 130/8= 16.25

So we now take each x value and minus 16.25 from it.

This gives us, (2-16.25)= -14.25; (6-16.25)= -10.25; (8-16.25)= -8.25; (12-16.25)= -4.25; (18-16.25)= 1.75; (24-16.25)= 7.75; (28-16.25)= 11.75; (32-16.25)= 15.75.

We then square all of these numbers to get, -14.252+ -10.252 + -8.252 + -4.252 + 1.752 + 7.752 + 11.752 + 15.752= 203.0625 + 105.0625 + 68.0625 + 18.0625 + 3.0625 + 60.0625 + 138.0625 + 248.0625 = 843.5

We now take this value and divide it by n.

This gives us, 843.5/8= 105.4375

The last thing is now to take the square root of 105.4375, which is 10.268276389.

So the numpy module is correct.

And this is how to compute the standard deviation in Python using the numpy module.


Related Resources

How to Randomly Select From or Shuffle a List in Python



HTML Comment Box is loading comments...