python 关于绘制ROC曲线和计算AUC

# -*- coding: utf-8 -*-
"""
Created on Tue Mar 24 09:06:48 2020

@author: guangjie2333
"""
import matplotlib.pyplot as plt
from sklearn.metrics import confusion_matrix
import numpy as np
import xlrd

"""
主函数
"""   

if __name__ == '__main__':
    stats_roc = xlrd.open_workbook("stats_roc.xls");
    sheet1 = stats_roc.sheets()[0]; #获取sheet1
    size = sheet1.nrows; #获取行数
    y_Actual = sheet1.col_values(0) [1:size]; #获取标签,第零行是标题,数据从第一行开始
    y_Predicted = sheet1.col_values(1) [1:size]; #获取预测值(置信度)
    size = size - 1;#因为第零行为标题,舍去
    
    threshold = 0;#阈值
    TPR_list = []; #ROC 的纵坐标 真阳率
    FPR_list = []; #ROC 的横坐标 假阳率
    
    
    while threshold < 1.005: # 设置阈值
        y_Predicted_new = [];
        for i in range(0,size): #二分法重置预测值
            if y_Predicted[i] >= threshold:
                y_Predicted_new.append(1);
            else :
                y_Predicted_new.append(0);
        cm = confusion_matrix(y_Actual, y_Predicted_new) ; #混淆矩阵
            
        #调试
        # print(y_Predicted_new);
        # print(threshold);
        # print(cm);
        # print(round(cm[1,1]/size,3));
        # print(round(cm[0,1]/size,3),"\n\n\n");
        
        
        TP = cm[1,1];
        TN = cm[0,0];
        FP = cm[0,1];
        FN = cm[1,0];
        FPR = round(FP / (FP + TN),3);#保留小数点后三位
        TPR = round(TP / (TP + FN),3);
        TPR_list.append(TPR); 
        FPR_list.append(FPR);
        
        threshold += 0.005;
        #while 结束
        
    #作图   
    plt.figure();
    plt.plot(FPR_list, TPR_list, color='green')  
    plt.plot([0, 1], [0, 1], color='navy', 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('Comparison of classification models')


    #用无穷小累加积分求AUC面积
    AUC = 0;
    for i in range(0,len(TPR_list)):
        AUC += 0.005 * TPR_list[i];
        
    print("AUC : ", round(AUC,3));    
        
        
        
        
        
        
        
        
        
        
        
        

在这里插入图片描述
AUC : 0.81

stats_roc.xls 如下
在这里插入图片描述

  • 0
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

鄢广杰

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

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

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

打赏作者

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

抵扣说明:

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

余额充值