利用python画出AUC曲线的实例


Posted in Python onFebruary 28, 2020

以load_breast_cancer数据集为例,模型细节不重要,重点是画AUC的代码。

直接上代码:

from sklearn.datasets import load_breast_cancer
from sklearn import metrics
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
import pylab as plt
import warnings;warnings.filterwarnings('ignore')
dataset = load_breast_cancer()
data = dataset.data
target = dataset.target
X_train,X_test,y_train,y_test = train_test_split(data,target,test_size=0.2)
rf = RandomForestClassifier(n_estimators=5)
rf.fit(X_train,y_train)
pred = rf.predict_proba(X_test)[:,1]
#############画图部分
fpr, tpr, threshold = metrics.roc_curve(y_test, pred)
roc_auc = metrics.auc(fpr, tpr)
plt.figure(figsize=(6,6))
plt.title('Validation ROC')
plt.plot(fpr, tpr, 'b', label = 'Val AUC = %0.3f' % roc_auc)
plt.legend(loc = 'lower right')
plt.plot([0, 1], [0, 1],'r--')
plt.xlim([0, 1])
plt.ylim([0, 1])
plt.ylabel('True Positive Rate')
plt.xlabel('False Positive Rate')
plt.show()

利用python画出AUC曲线的实例

补充拓展:Python机器学习中的roc_auc曲线绘制

废话不多说,直接上代码

from sklearn.metrics import roc_curve,auc

from sklearn.ensemble import RandomForestClassifier

import matplotlib.pyplot as plt

from sklearn.model_selection import train_test_split

x_train,y_train,x_test,y_test=train_test_split(x,y,test_size=0.2)

rf=RandomForestClassifier()

rf.fit(x_train,y_train)

rf.score(x_train,y_train)

print('trainscore:'+str(rfbest.score(x_train,y_train)))
print('testscore:'+str(rfbest.score(x_test,y_test)))

y_score=rfbest.fit(x_train,y_train).predict_proba(x_test) #descision_function()不可用

print(type(y_score))

fpr,tpr,threshold=roc_curve(y_test,y_score[:, 1])
roc_auc=auc(fpr,tpr)
plt.figure(figsize=(10,10))
plt.plot(fpr, tpr, color='darkorange',
lw=2, label='ROC curve (area = %0.2f)' % roc_auc) ###假正率为横坐标,真正率为纵坐标做曲线
plt.plot([0, 1], [0, 1], color='navy', lw=2, linestyle='--')
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('Receiver operating characteristic example')
plt.legend(loc="lower right")
plt.show()

以上这篇利用python画出AUC曲线的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python入门之modf()方法的使用
May 15 Python
在Python中处理字符串之ljust()方法的使用简介
May 19 Python
Python读写unicode文件的方法
Jul 10 Python
Python的Django框架安装全攻略
Jul 15 Python
python 生成器生成杨辉三角的方法(必看)
Apr 10 Python
python+requests+unittest API接口测试实例(详解)
Jun 10 Python
Python基于贪心算法解决背包问题示例
Nov 27 Python
详谈python在windows中的文件路径问题
Apr 28 Python
浅谈Django中的数据库模型类-models.py(一对一的关系)
May 30 Python
Django DRF路由与扩展功能的实现
Jun 03 Python
python元组拆包实现方法
Feb 28 Python
python基础之停用词过滤详解
Apr 21 Python
Python编程快速上手——选择性拷贝操作案例分析
Feb 28 #Python
AUC计算方法与Python实现代码
Feb 28 #Python
Python编程快速上手——Excel表格创建乘法表案例分析
Feb 28 #Python
Python计算IV值的示例讲解
Feb 28 #Python
Python编程快速上手——PDF文件操作案例分析
Feb 28 #Python
Python自动采集微信联系人的实现示例
Feb 28 #Python
python代码实现TSNE降维数据可视化教程
Feb 28 #Python
You might like
新的一年,新的期待:DC在2020年的四部动画电影
2020/01/01 欧美动漫
同时提取多条新闻中的文本一例
2006/10/09 PHP
php数组键值用法实例分析
2015/02/27 PHP
php动态添加url查询参数的方法
2015/04/14 PHP
PHP实现webshell扫描文件木马的方法
2017/07/31 PHP
用正则表达式 动态创建/增加css style script 兼容IE firefox
2009/03/10 Javascript
左侧是表头的JS表格控件(自写,网上没有的)
2013/06/04 Javascript
jquery插件之定时查询待处理任务数量
2014/05/01 Javascript
javascript动态修改Li节点值的方法
2015/01/20 Javascript
JS控制静态页面传递参数并获取参数应用
2016/08/10 Javascript
使用bootstrap实现多窗口和拖动效果
2016/09/22 Javascript
微信小程序 template模板详解及实例代码
2017/03/09 Javascript
Angularjs单选框相关的示例代码
2017/08/17 Javascript
keep-alive保持组件状态的方法
2020/12/02 Javascript
学习python (1)
2006/10/31 Python
python实现获取Ip归属地等信息
2016/08/27 Python
使用python绘制3维正态分布图的方法
2018/12/29 Python
如何使用Python进行OCR识别图片中的文字
2019/04/01 Python
flask 实现token机制的示例代码
2019/11/07 Python
使用apiDoc实现python接口文档编写
2019/11/19 Python
Jupyter notebook无法导入第三方模块的解决方式
2020/04/15 Python
Python内置异常类型全面汇总
2020/05/28 Python
PyChon中关于Jekins的详细安装(推荐)
2020/12/28 Python
CSS3 重置iphone浏览器按钮input,select等表单元素的默认样式
2014/10/11 HTML / CSS
前端实现弹幕效果的方法总结(包含css3和canvas的实现方式)
2018/07/12 HTML / CSS
联想德国官网:Lenovo Germany
2018/07/04 全球购物
面向对象编程的优势是什么
2015/12/17 面试题
生日派对邀请函
2014/01/13 职场文书
留学经费担保书
2014/05/12 职场文书
商务经理岗位职责
2014/07/30 职场文书
乡镇党的群众路线对照检查材料
2014/09/24 职场文书
学习雷锋精神活动总结
2015/02/06 职场文书
在校证明模板
2015/06/17 职场文书
Jupyter Notebook内使用argparse报错的解决方案
2021/06/03 Python
Nginx源码编译安装过程记录
2021/11/17 Servers
Mysql存储过程、触发器、事件调度器使用入门指南
2022/01/22 MySQL