import math
list1=[12,13,15,11,9,12,13,10,11,12,13,7,8]
def my_stddev(my_list):
my_sum=0
for i in my_list:
my_sum=my_sum+i
#print(my_sum)
print("Sum :",my_sum)
my_no=len(my_list)
my_avg=my_sum/my_no
print("Average",my_avg)
sum_avg=0.0
for i in my_list:
sum_avg=sum_avg+math.pow((i-my_avg),2)
my_variance = sum_avg/my_no
print("Variance :",my_variance )
std=math.sqrt(my_variance)
return std
print("Standard Deviation",my_stddev(list1))
Output
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.
list1=[12,13,15,11,9,12,13,10,11,12,13,7,8]
my_dict={'l1':list1}
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.
import numpy as np
my_data=np.array(list1)
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
import statistics
print("Standard Deviation : ",statistics.stdev(list1))
Output is here
Standard Deviation : 2.24179415327122
The statistics library uses sample Standar Deviation.
Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.