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

在 Matlab 中我用

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

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

305060 次浏览

The docs indicate that numpy.correlate is not what you are looking for:

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.

Instead, as the other comments suggested, you are looking for a Pearson correlation coefficient. To do this with scipy try:

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

This gives

(0.99339926779878274, 0.073186395040328034)

You can also use numpy.corrcoef:

import numpy
print(numpy.corrcoef(a,b))

This gives:

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