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

yipeiwu_com6年前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+pandas计算数据相关系数的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python使用post及get方式提交数据的实例

最近在使用Python的过程中,发现网上很少提到在使用post方式时,怎么传一个数组作为参数的示例,此处根据自己的实践经验,给出相关示例: 单纯的post请求: def http_p...

浅谈Python peewee 使用经验

本文使用案例是基于 python2.7 实现 以下内容均为个人使用 peewee 的经验和遇到的坑,不会涉及过多的基本操作。所以,没有使用过 peewee,可以先阅读文档 正确性和覆盖面...

python 文件的基本操作 菜中菜功能的实例代码

python  文件的基本操作 菜中菜 文件操作 ​ open():打开 ​ file:文件的位置(路径) ​ mode:操作文件模式 &#...

Python使用ntplib库同步校准当地时间的方法

NTP(Network Time Protocol)是由美国德拉瓦大学的David L. Mills教授于1985年提出,设计用来在Internet上使不同的机器能维持相同时间的一种通讯...

获取django框架orm query执行的sql语句实现方法分析

获取django框架orm query执行的sql语句实现方法分析

本文实例讲述了获取django框架orm query执行的sql语句实现方法。分享给大家供大家参考,具体如下: 利用Django orM 可以很方便的写出很多查询,但有时候,我们需要检查...