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


例子:
代码:

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= ", Mat1)
print("矩阵2= ", Mat2)
print("相关系数矩阵= ", 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.


————————————————
版权声明:本文为CSDN博主「Hello_xzy_Word」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_39514033/article/details/88931639

原文地址:https://www.cnblogs.com/wcxia1985/p/14807424.html