首页 文章

Numpy相关混淆[重复]

提问于
浏览
1

这个问题在这里已有答案:

我想计算2个数组之间的相关性 . 为此,我想使用NumPy .

我在一个小例子上使用了 numpy.correlate 函数:

import numpy as np

a = [1, 2, 3]
b = [2, 3, 4]

np.correlate(a, b)
>>> np.array([20])

我真的不知道如何解释这个结果 . 我想要的是一个介于-1和1之间的数字来表示相关性,1表示数组是正相关的,-1表示数组是负相关的 .

我怎么能得到这个号码?

1 回答

  • 4

    你're using the wrong function. You'正在寻找numpy.corrcoef,它实际上计算了相关系数 .

    a = [1, 2, 3]
    b = [2, 3, 4]
    
    >>> np.corrcoef(a, b)
    array([[ 1.,  1.],
           [ 1.,  1.]])
    

    Hooked As mentioned,它返回协方差矩阵的值矩阵 .

    如果您想要Pearson相关系数,可以使用 pearsonr 来自 scipy.stats.stats . Hooked's answer here是此方法的正确实现 .

相关问题