当前位置:主页 > 软件编程 > Python代码 >

Python+pandas计算数据相关系数的实例

时间:2021-05-28 08:03:04 | 栏目:Python代码 | 点击:

本文主要演示pandas中DataFrame对象corr()方法的用法,该方法用来计算DataFrame对象中所有列之间的相关系数(包括pearson相关系数、Kendall Tau相关系数和spearman秩相关)。

>>> import numpy as np
>>> import pandas as pd

>>> df = pd.DataFrame({'A':np.random.randint(1, 100, 10),
   'B':np.random.randint(1, 100, 10),
   'C':np.random.randint(1, 100, 10)})
>>> df
   A  B  C
0  5 91  3
1 90 15 66
2 93 27  3
3 70 44 66
4 27 14 10
5 35 46 20
6 33 14 69
7 12 41 15
8 28 62 47
9 15 92 77
>>> df.corr() # pearson相关系数
     A       B       C
A 1.000000 -0.560009 0.162105
B -0.560009 1.000000 0.014687
C 0.162105 0.014687 1.000000
>>> df.corr('kendall') # Kendall Tau相关系数

     A       B       C
A 1.000000 -0.314627 0.113666
B -0.314627 1.000000 0.045980
C 0.113666 0.045980 1.000000
>>> df.corr('spearman') # spearman秩相关

     A       B       C
A 1.000000 -0.419455 0.128051
B -0.419455 1.000000 0.067279
C 0.128051 0.067279 1.000000

您可能感兴趣的文章:

相关文章