Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

correlation matrix in python

Tags:

python

How do I calculate correlation matrix in python? I have an n-dimensional vector in which each element has 5 dimension. For example my vector looks like

 [  [0.1, .32, .2,  0.4, 0.8],   [.23, .18, .56, .61, .12],   [.9,   .3,  .6,  .5,  .3],   [.34, .75, .91, .19, .21] ]  

In this case dimension of the vector is 4 and each element of this vector have 5 dimension. How to construct the matrix in the easiest way?

Thanks

like image 724
user1964587 Avatar asked Feb 02 '13 00:02

user1964587


People also ask

What is correlation matrix in Python?

A correlation matrix is a table containing correlation coefficients between variables. Each cell in the table represents the correlation between two variables. The value lies between -1 and 1.

How do you plot a correlation matrix in Python?

You can plot correlation between two columns of pandas dataframe using sns. regplot(x=df['column_1'], y=df['column_2']) snippet. You can see the correlation of the two columns of the dataframe as a scatterplot.

What is correlation matrix pandas?

Correlation is a statistical technique that shows how two variables are related. Pandas dataframe. corr() method is used for creating the correlation matrix. It is used to find the pairwise correlation of all columns in the dataframe. Any na values are automatically excluded.

What is the correlation of a matrix?

A correlation matrix is simply a table which displays the correlation coefficients for different variables. The matrix depicts the correlation between all the possible pairs of values in a table. It is a powerful tool to summarize a large dataset and to identify and visualize patterns in the given data.


2 Answers

Using numpy, you could use np.corrcoef:

In [88]: import numpy as np  In [89]: np.corrcoef([[0.1, .32, .2, 0.4, 0.8], [.23, .18, .56, .61, .12], [.9, .3, .6, .5, .3], [.34, .75, .91, .19, .21]]) Out[89]:  array([[ 1.        , -0.35153114, -0.74736506, -0.48917666],        [-0.35153114,  1.        ,  0.23810227,  0.15958285],        [-0.74736506,  0.23810227,  1.        , -0.03960706],        [-0.48917666,  0.15958285, -0.03960706,  1.        ]]) 
like image 61
unutbu Avatar answered Oct 12 '22 23:10

unutbu


You can also use np.array if you don't want to write your matrix all over again.

import numpy as np a = np.array([ [0.1, .32, .2,  0.4, 0.8], [.23, .18, .56, .61, .12], [.9,   .3,  .6,  .5,  .3],  [.34, .75, .91, .19, .21]])  b = np.corrcoef(a) print b 
like image 29
Daniel González Cortés Avatar answered Oct 12 '22 23:10

Daniel González Cortés