Python Numpy库 numpy.corrcoef()函数讲解

Python Numpy库 numpy.corrcoef()函数讲解_第1张图片

例子:

代码:

import numpy as np

Array1 = [[1, 2, 3], [4, 5, 6]]
Array2 = [[11, 25, 346], [734, 48, 49]]
Mat1 = np.array(Array1)
Mat2 = np.array(Array2)
correlation = np.corrcoef(Mat1, Mat2)
print("矩阵1=\n", Mat1)
print("矩阵2=\n", Mat2)
print("相关系数矩阵=\n", correlation)

 结果:

矩阵1=
 [[1 2 3]
 [4 5 6]]
矩阵2=
 [[ 11  25 346]
 [734  48  49]]
相关系数矩阵=
 [[ 1.          1.          0.88390399 -0.86539304]
 [ 1.          1.          0.88390399 -0.86539304]
 [ 0.88390399  0.88390399  1.         -0.53057867]
 [-0.86539304 -0.86539304 -0.53057867  1.        ]]

Process finished with exit code 0

可以看出函数的返回值还是一个矩阵

结果矩阵的行数*结果矩阵的列数==矩阵1的行数*矩阵2的行数

令:

     0=[1 2 3]    1=[4 5 6]    2=[11 25 346]    3=[734 48 49] 

矩阵中值的意义:

                       0列                          1列                             2列                             3列

0行             0 0 相关性             0 1 相关性                  0 2 相关性                  0 3 相关性      

1行             1 0 相关性             1 1 相关性                  1 2 相关性                  1 3 相关性   

2行             2 0 相关性             2 1 相关性                  2 2 相关性                   2 3 相关性   

3行             3 0 相关性             3 1 相关性                  3 2 相关性                   3 3 相关性   

 

自己和自己的相关性最大,值为1,所以对角线的值全为1.

Python Numpy库 numpy.corrcoef()函数讲解_第2张图片

你可能感兴趣的:(numpy,python,corrcoef())