问题描述
np.corrcoef返回一个矩阵对我来说似乎很奇怪.
It seems strange to me that np.corrcoef returns a matrix.
correlation1 = corrcoef(Strategy1Returns,Strategy2Returns)
[[ 1. -0.99598935]
[-0.99598935 1. ]]
有人知道为什么会这样吗,以及是否有可能仅返回经典意义上的一个值?
Does anyone know why this is the case and whether it is possible to return just one value in the classical sense?
推荐答案
它允许您计算> 2个数据集的相关系数,例如
It allows you to compute correlation coefficients of >2 data sets, e.g.
>>> from numpy import *
>>> a = array([1,2,3,4,6,7,8,9])
>>> b = array([2,4,6,8,10,12,13,15])
>>> c = array([-1,-2,-2,-3,-4,-6,-7,-8])
>>> corrcoef([a,b,c])
array([[ 1. , 0.99535001, -0.9805214 ],
[ 0.99535001, 1. , -0.97172394],
[-0.9805214 , -0.97172394, 1. ]])
在这里,我们可以立即获得a,b(0.995),a,c(-0.981)和b,c(-0.972)的相关系数.两个数据集的情况只是N数据集类的一个特例.并且可能最好保持相同的返回类型.由于单一值"可以简单地通过
Here we can get the correlation coefficient of a,b (0.995), a,c (-0.981) and b,c (-0.972) at once. The two-data-set case is just a special case of N-data-set class. And probably it's better to keep the same return type. Since the "one value" can be obtained simply with
>>> corrcoef(a,b)[1,0]
0.99535001355530017
没有特殊理由创建特殊情况.
there's no big reason to create the special case.
这篇关于为什么corrcoef返回矩阵?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!