计算三个向量Pearson Correlation

时间:2018-06-05 00:19:50

标签: python math correlation pearson-correlation

我想计算三个向量的Pearson相关性。像:

np.corrcoef(X,Y,Z)

任何人都知道如何在Python中这样做?有没有图书馆?

非常感谢!

1 个答案:

答案 0 :(得分:0)

创建二维数组:

import numpy as np

X = np.random.randint(0,100,100)
Y = np.random.randint(0,100,100)
Z = np.random.randint(0,100,100)
spam = np.array([X,Y,Z])

然后运行np.corrcoeff:

eggs = np.corrcoef(spam)

哪个给出了

spam
array([[12,  1, 23, 95, 58, 37, 38, 76, 99, 24, 50, 47, 29, 67, 38, 62,
        28, 39, 26, 32, 55, 11, 67, 71, 71, 50,  9, 54, 59, 22, 70, 63,
        11, 43, 30, 54, 14, 11, 89, 68, 98, 27, 18, 66, 14, 23, 83, 81,
        24, 90, 56, 40,  3, 94, 86, 54, 66, 68, 96, 74, 46, 19, 58, 74,
         7, 73, 93, 91, 10, 75, 32, 91, 45, 57, 81, 96, 20, 25, 18, 86,
        59, 58, 94, 97, 10, 11,  6, 30, 28, 76, 56, 86, 64, 38, 26, 25,
        26, 77, 76,  0],
       [76, 54, 66, 16, 97, 22, 66, 59, 90, 67, 85, 58, 49, 13, 36, 44,
        91, 20, 63,  2,  1, 51, 66, 12, 32, 10, 48, 88,  6, 96, 68, 60,
        22, 24, 51,  2, 94, 95, 98, 79, 24, 22, 94, 69, 36, 37, 50, 94,
         8,  2, 15, 80, 99, 77, 15, 95, 76, 25,  0, 69, 46, 21, 47, 87,
        88,  6, 34, 63, 46, 12, 62,  4, 36, 89, 21, 44, 96, 22, 11, 14,
        63, 55, 70, 58, 33, 93, 88, 39,  0, 18,  9, 92, 37, 13, 12, 67,
        98, 34,  9,  6],
       [56, 21, 61, 55, 82, 60, 13, 36, 17, 52, 93, 88, 65, 29, 67, 81,
        65, 59, 33, 89, 58, 99, 78,  1, 75, 39, 10, 75, 94, 57, 42, 32,
        19,  7, 77, 82, 96, 61, 94, 50, 61, 13, 21, 51, 70, 14, 60, 79,
        32, 29, 22, 93, 31, 19, 74, 37, 85, 74, 50, 50, 37,  5, 33, 39,
        75, 32, 43, 10, 74, 39, 74, 26, 57, 38, 79, 90, 63, 13, 53, 57,
        44,  4, 13, 39, 14, 97, 21, 81,  2, 49, 72,  5, 72, 32, 63, 37,
        53, 31,  8, 12]])
eggs
array([[ 1.        , -0.14396307, -0.01107133],
       [-0.14396307,  1.        ,  0.17098877],
       [-0.01107133,  0.17098877,  1.        ]])