Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages
wb_sunny

Find mean and standard deviation in Python

Mean And Sd

Mean and standard deviation are two important metrics in Statistics.

  • Mean is sum of all the entries divided by the number of entries.
  • Standard deviation is a measure of the amount of variation or dispersion of a set of values.

Let’s look at the steps required in calculating the mean and standard deviation.

Steps to calculate Mean

  1. Take the sum of all the entries.
  2. Divide the sum by the number of entries.

Steps to calculate Standard Deviation

  1. Calculate the mean as discussed above.
  2. Calculate variance for each entry by subtracting the mean from the value of the entry. Then square each of those resulting values and sum the results. Then divide the result by the number of data points minus one. This will give the variance.
  3. The square root of the variance (calculated above) is then used to find the standard deviation.

Multiple Methods to Find the Mean and Standard Deviation in Python

Let’s write a Python code to calculate the mean and standard deviation. You get multiple options for calculating mean and standard deviation in python. Let’s look at the inbuilt statistics module and then try writing our own implementation.

1. Using statistics module

This module provides you the option of calculating mean and standard deviation directly.

Let’s start by importing the module.

import statistics 

Let’s declare an array with dummy data.

data = [7,5,4,9,12,45] 

Now to calculate the mean of the sample data, use:

statistics.mean(data)

This statement will return the mean of the data. We can print mean in the output using:

print("Mean of the sample is % s " %(statistics.mean(data))) 

We get the output as:

Mean of the sample is 13.666666666666666 

If you are using an IDE for coding you can hover over the statement and get more information on statistics.mean().

Mean

Alternatively you can read the documentation here.

To calculate the standard deviation of the sample data use:

print("Standard Deviation of the sample is % s "%(statistics.stdev(data)))

We get the output as:

Standard Deviation of the sample is 15.61623087261029

Here’s a brief documentation of statistics.stdev().

Stdev

Complete Code to Find Standard Deviation and Mean

The complete code for the snippets above is as follows :

import statistics 
import numpy as np

data = np.array([7,5,4,9,12,45])

print("Standard Deviation of the sample is % s "% (statistics.stdev(data)))
print("Mean of the sample is % s " % (statistics.mean(data))) 

2. Write your own function

Let’s write our function to calculate mean.

def mean(data):
  n = len(data)
  mean = sum(data) / n
  return mean

This function will calculate the mean.

Now let’s write a function to calculate standard deviation.

This can be a little tricky so let’s go about it step by step.

The standard deviation is the square root of variance. So we can write two functions:

  • one that will calculate variance
  • one that will calculate the square root of variance.

The function for calculating variance is as follows:

def variance(data):
  
  n = len(data)
  
  mean = sum(data) / n
  
  deviations = [(x - mean) ** 2 for x in data]
   
  variance = sum(deviations) / n
  return variance

You can refer to the steps given at the beginning of the tutorial to understand the code.

Now we can write a function that calculates the square root of variance.

def stdev(data):
  import math
  var = variance(data)
  std_dev = math.sqrt(var)
  return std_dev

Complete Code

The complete code is as follows :

import numpy as np #for declaring an array

def mean(data):
  n = len(data)
  mean = sum(data) / n
  return mean

def variance(data):
  n = len(data)
  mean = sum(data) / n
  deviations = [(x - mean) ** 2 for x in data]
  variance = sum(deviations) / n
  return variance

def stdev(data):
  import math
  var = variance(data)
  std_dev = math.sqrt(var)
  return std_dev

data = np.array([7,5,4,9,12,45])

print("Standard Deviation of the sample is % s "% (stdev(data)))
print("Mean of the sample is % s " % (mean(data))) 

Conclusion

This tutorial was about calculating mean and standard deviation in python. Hope you had fun learning with us!