    # Cara menggunakan covariance matrix python

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.

### 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:

Either the covariance between x and y is :

Covariance(x,y) > 0 : this means that they are positively related

Covariance(x,y) < 0 : this means that x and y are negatively  related

if Covariance(x,y) = 0 : then x and y are independent of each other.

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:

ny.cov(m, y=None, rowvar=True, bias=False, ddof=None, fweights=None, aweights=None)

m : [array_like] A 1D or 2D variables. variables are columns

y : [array_like] It has the same form as that of m.

rowvar : [bool, optional] If rowvar is True (default), then each row represents a variable, with observations in the columns. Otherwise, the relationship is transposed:

bias : Default normalization is False. If bias is True it normalize the data points.

ddof : If not None the default value implied by bias is overridden. Note that ddof=1 will return the unbiased estimate, even if both fweights and aweights are specified.

fweights : fweight is 1-D array of integer frequency weights

aweights : aweight is 1-D array of observation vector weights.

Returns: It returns ndarray covariance matrix

### 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:

numpy.corrcoef(x, y=None, rowvar=True, bias=<no value>, ddof=<no value>)

x :  A 1-D or 2-D array containing multiple variables and observations. Each row of x represents a variable, and each column a single observation of all those variables. Also see rowvar below.

y, optional: An additional set of variables and observations. y has the same shape as x.

rowvar : If rowvar is True (default), then each row represents a variable, with observations in the columns. Otherwise, the relationship is transposed: each column represents a variable, while the rows contain observations.

Returns: ndarray

### So Why do we need Correlation ?

1. Covariance tells us if two random variables are +ve or -ve related it doesn’t tell us by how much.
2. Covariance is variant to arithmetic changes eg: if we multiply x by 10 or divide by 10 then the result will change, this is not true for correlation where the results remain unchanged by such operations.
3. The output of covariance is difficult to compare as the values can range from – infinity to +infinity. While  output values of correlation ranges from 0 to 1.

### Relation Between Correlation and Covariance

Correlation 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.

## Python3

`import` `numpy as np`

`import` `pandas as pd`

`# loading in the iris dataset for demo purposes`

`dataset``=` `pd.read_csv(``"iris.csv"``)`

`numpy as np`0 In this example we won’t be using the target column

## Python3

`numpy as np`1`=` `numpy as np`3`numpy as np`4`numpy as np`5`numpy as np`6

Program to implement covariance matrix:

## Python3

`numpy as np`7

`numpy as np`8 `numpy as np`9

`import`0`import`1`=` `import`3

`import`0`import`5`=` `import`7`import`8

`import`0`pandas as pd`0 `pandas as pd`1`pandas as pd`2`numpy as np`4 `pandas as pd`4`pandas as pd`5 `pandas as pd`6`numpy as np`4 `pandas as pd`8`pandas as pd`9 `import`5 