Standard Deviation, Variance and Covariance

Standard Deviation

Standard Deviation Variance and Covariance

Standard deviation, variance and covariance have very important applications in machine learning and data science. Further, they are closely related to each other. In feature reduction techniques, such as PCA ( Principle Component Analysis) features are selected based on  high variance.  In this post I will explain standard deviation, variance and covariance. I will also demonstrate how to compute standard deviation, variance and covariance in Python.

Standard Deviation

Standard Deviation

Standard deviation shows how data is spread about mean.  In other words, it measures the scantness  in a data set.

It is denoted by  σ and formula for standard deviation is

σ =  √|xi-mean|/(n-1)

xi is data series

n is the number of  data points

Python Code for  Standard Deviation

import statistics
data = [5,15,25,35,45]
sd=statistics.stdev(data)
m=statistics.mean(data)
print(“Mean”,m)
print(“Standard Deviation”,sd)

Output :

Mean 25
Standard Deviation 15.811388300841896

 

Variance

Variance is square of standard deviation which is

Variance=  σ2

Python Code for Variance

import statistics
data = [5,15,25,35,45]
sd=statistics.stdev(data)
m=statistics.mean(data)
v= statistics.variance(data)
print(“Mean”,m)
print(“Standard Deviation”,sd)
print(“Variance”,v)

Output:

Mean 25
Standard Deviation 15.811388300841896
Variance 250

Covariance

Covariance is  used to  measure variability between two variables. Suppose X and Y be two variables then covariance between X and Y is

Cov(X,Y) = (X-MeanX) (Y-MeanY)/ n-1

Let us calculate covariance matrix  in Python

Python Code for Covariance Matrix

import numpy as np
data = np.array([[5,15,25,35,45,65],[20, 35,40,50,60,70] ])
covmat=np.cov(data)
print(“Covarinace Matrix of X and Y”, covmat)

Output:

Covarinace Matrix of X and Y

[[466.66666667 383.33333333] [383.33333333 324.16666667]]

Conclusion-

In this post, we have explained about standard deviation, variance, and covariance. These concepts are very useful applications in machine learning, data science and data analytics. Hope it is  useful for you and you will apply.

References-

Share to Your Friend
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Be the first to comment on "Standard Deviation, Variance and Covariance"

Leave a comment

Your email address will not be published.


*