欢迎来到代码驿站!

Python代码

当前位置:首页 > 软件编程 > 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

上一篇:Python BS4库的安装与使用详解

栏    目:Python代码

下一篇:python支持断点续传的多线程下载示例

本文标题:Python+pandas计算数据相关系数的实例

本文地址:http://www.codeinn.net/misctech/130776.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有