Standard Deviation σ

Standard Deviation

  1. Find out the average value (μ ) of the numbers or the list
  2. Find out the sum (Σ) of square of difference between number and average value
  3. Divide the sum by number of elements ( N )
  4. Take the square root of the above division
We will create a Python function to return the Standard Deviation.
Note that this is Population Standard Deviation
import math
def my_stddev(my_list):
    for i in my_list:
    print("Sum :",my_sum)
    for i in my_list:
    my_variance = sum_avg/my_no
    print("Variance :",my_variance )    
    return std
print("Standard Deviation",my_stddev(list1))
Sum : 146
Average 11.23076923076923
Variance : 4.6390532544378695
Standard Deviation 2.1538461538461537
We will compare the Standard Deviation values by using Pandas, Numpy and Python statistics library. Note the difference in values as there are two different formulas to get the Standard Deviation. The Population method uses N and Sample method uses N - 1, where N is the total number of elements.

Using Pandas

Read more on Pandas here. We can use ddof option to tell Delta Degrees of Freedom. Default value of ddof is 1. The code with output is here.
my_data = pd.DataFrame(data=my_dict)
print(my_data['l1'].std(ddof=0))  # 2.1538461538461537
print(my_data['l1'].std(ddof=1))  # 2.24179415327122
Note that we are getting the same value in Python and Pandas by using ddof=0 in Pandas std() function.

Using Numpy

Read more on Numpy here. There is a built in standar deviation function in Numpy.
import numpy as np
print(my_data.std(ddof=0)) # 2.153846153846154
print(my_data.std(ddof=1)) # 2.2417941532712202
Here also we are getting same value as Python by using ddof=0

Using statistics

We will use the statistics library
import statistics 
print("Standard Deviation : ",statistics.stdev(list1))
Output is here
Standard Deviation :  2.24179415327122
The statistics library uses sample Standar Deviation.
Subscribe to our YouTube Channel here


* indicates required
Subscribe to plus2net

    Post your comments , suggestion , error , requirements etc here

    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2022 All rights reserved worldwide Privacy Policy Disclaimer