如何在python中获得两个向量的相关性

时间:2013-10-17 13:25:50

标签: python numpy

在matlab中我使用

a=[1,4,6]
b=[1,2,3]
corr(a,b)

返回.9934。我试过numpy.correlate,但它会返回完全不同的东西。获得两个向量的相关性的最简单方法是什么?

1 个答案:

答案 0 :(得分:132)

文档表明numpy.correlate不是您想要的:

numpy.correlate(a, v, mode='valid', old_behavior=False)[source]
  Cross-correlation of two 1-dimensional sequences.
  This function computes the correlation as generally defined in signal processing texts:
     z[k] = sum_n a[n] * conj(v[n+k])
  with a and v sequences being zero-padded where necessary and conj being the conjugate.

相反,正如其他评论建议的那样,您正在寻找Pearson correlation coefficient。要用scipy试试这个:

from scipy.stats.stats import pearsonr   
a = [1,4,6]
b = [1,2,3]   
print pearsonr(a,b)

这给出了

(0.99339926779878274, 0.073186395040328034)

您还可以使用numpy.corrcoef

import numpy
print numpy.corrcoef(a,b)

这给出了:

[[ 1.          0.99339927]
 [ 0.99339927  1.        ]]