In this article, we will be discussing the relationship between Covariance and Correlation and program our own function for calculating covariance and correlation using python. Show Covariance:It tells us how two quantities are related to one another say we want to calculate the covariance between x and y the then the outcome can be one of these. where are the means of x and y respectively. Interpreting the output:
Covariance matrix: Covariance provides a measure of the strength of correlation between two variable or more set of variables, to calculate the covariance matrix, the cov() method in numpy is used.. Syntax:
Correlation:It shows whether and how strongly pairs of variables are related to each other. Correlation takes values between -1 to +1, wherein values close to +1 represents strong positive correlation and values close to -1 represents strong negative correlation. It gives the direction and strength of the relationship between variables. Correlation Matrix: It is basically a covariance matrix. Also known as the auto-covariance matrix, dispersion matrix, variance matrix, or variance-covariance matrix. It is a matrix in which i-j position defines the correlation between the ith and jth parameter of the given data-set. It is calculated using numpy‘s corrcoeff() method. Syntax:
So Why do we need Correlation ?
Relation Between Correlation and CovarianceCorrelation is just normalized Covariance refer to the formula below. where are the standard deviation of x and y respectively. Python Program to convert Covariance matrix to Correlation matrix To solve this problem we have selected the iris data because to compute covariance we need data and it’s better if we use a real word example dataset. Loading and displaying the dataset Python3
In this example we won’t be using the target column Python3
Program to implement covariance matrix: Python3
|