python实现LOOCV并画ROC曲线

以sklearn中的iris数据为例
用的是Adaboost算法

# -*- coding: utf-8 -*-
"""
Created on Thu Jul  4 21:17:19 2019

@author: ZQQ
"""
import numpy as np
from sklearn.ensemble import AdaBoostClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import LeaveOneOut 
from sklearn import  datasets


iris = datasets.load_iris()
X = iris.data
y = iris.target

##变为2分类
X, y = X[y != 2], y[y != 2]
# 这个地方可以加上上一篇博客的随机打乱数据操作
loo = LeaveOneOut()
loo.get_n_splits(X)
print("交叉验证次数:",loo.get_n_splits(X)) # 输出为100,--->进行100折,也就是留一

y_pred = []
for train_index, test_index in loo.split(X):
    #print("train:", train_index, "TEST:", test_index) # 索引
    X_train, X_test = X[train_index], X[test_index]
    y_train, y_test = y[train_index], y[test_index]
    #print(X_train, X_test, y_train, y_test)
    
    # 调用、训练模型
    model_bdt = AdaBoostClassifier(DecisionTreeClassifier(max_depth = 2), algorithm = "SAMME", n_estimators = 10)
    model_bdt.fit(X_train, y_train)
   
    # 预测
    x_test_pred = model_bdt.predict(X_test)
    
    #print(x_test_pred)
    y_pred.append(x_test_pred) # 当前预测值添加到列表中
  
from sklearn.metrics import roc_curve, auc

y_pred = np.array(y_pred) # list to array
fpr, tpr, threshold = roc_curve(y, y_pred)  #计算真正率和假正率
roc_auc = auc(fpr, tpr)  # 计算auc的值

import matplotlib.pyplot as plt
lw = 2 # 定义线条宽度
plt.figure(figsize=(8, 5))
plt.plot(fpr, tpr, color='darkorange',
         lw=lw, label='ROC curve (area = %0.2f)' % roc_auc)  ###假正率为横坐标,真正率为纵坐标做曲线
plt.plot([0, 1], [0, 1], color='navy', lw=lw, 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.savefig('loocv.png',dpi=600) # 以600大批保存图片
plt.show()

当然还有其他风格的代码,实现的功能是相同的。

参考官网教程:
https://scikit-learn.org/stable/auto_examples/model_selection/plot_roc.html#sphx-glr-auto-examples-model-selection-plot-roc-py

https://scikit-learn.org/stable/modules/generated/sklearn.metrics.roc_auc_score.html#sklearn.metrics.roc_auc_score

https://my.oschina.net/u/3702502/blog/1841599

  • 3
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

机器不学习我学习

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值