python中的相关矩阵
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14657433/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
correlation matrix in python
提问by user1964587
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
如何在python中计算相关矩阵?我有一个 n 维向量,其中每个元素都有 5 维。例如我的向量看起来像
[ [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?
在这种情况下,向量的维数是 4,这个向量的每个元素都有 5 维。如何以最简单的方式构造矩阵?
Thanks
谢谢
回答by unutbu
Using numpy, you could use np.corrcoef:
使用numpy,您可以使用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. ]])
回答by user5733162
Here is a pretty good exampleof calculating a correlations matrix form multiple time series using Python. Included source code calculates correlation matrix for a set of Forex currency pairs using Pandas, NumPy, and matplotlib to produce a graph of correlations.
这是使用 Python 从多个时间序列计算相关矩阵的一个很好的例子。包含的源代码使用 Pandas、NumPy 和 matplotlib 计算一组外汇货币对的相关矩阵,以生成相关图。
Sample data is a set of historical data files, and the output is a single correlation matrix and a plot. The code is very well documented.
样本数据是一组历史数据文件,输出是单个相关矩阵和图。该代码有很好的文档记录。
回答by Daniel González Cortés
You can also use np.array if you don't want to write your matrix all over again.
如果您不想重新编写矩阵,也可以使用 np.array 。
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
回答by Marcus V.
As I almost missed that comment by @Anton Tarasenko, I'll provide a new answer. So given your array:
由于我几乎错过了@Anton Tarasenko 的评论,我将提供一个新答案。所以考虑到你的数组:
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]])
If you want the correlation matrix of your dimensions (columns), which I assume, you can use numpy (note the transpose!):
如果您想要我假设的维度(列)的相关矩阵,您可以使用 numpy(注意转置!):
import numpy as np
print(np.corrcoef(a.T))
Or if you have it in Pandas anyhow:
或者,如果您在 Pandas 中拥有它:
import pandas as pd
print(pd.DataFrame(a).corr())
Both print
都打印
array([[ 1. , -0.03783885, 0.34905716, 0.14648975, -0.34945863],
[-0.03783885, 1. , 0.67888519, -0.96102583, -0.12757741],
[ 0.34905716, 0.67888519, 1. , -0.45104803, -0.80429469],
[ 0.14648975, -0.96102583, -0.45104803, 1. , -0.15132323],
[-0.34945863, -0.12757741, -0.80429469, -0.15132323, 1. ]])

