python如何绘制roc曲线_如何在Python中绘制ROC曲线

本文介绍了如何在Python中使用sklearn和相关库绘制ROC曲线,包括使用sklearn.metrics的roc_curve函数计算FPR和TPR,以及利用matplotlib和ggplot进行可视化。示例代码展示了从预测概率到ROC曲线和AUC计算的完整过程。
摘要由CSDN通过智能技术生成

uniquegino..

70

假设您model是一个sklearn预测器,您可以尝试以下两种方法:

import sklearn.metrics as metrics

# calculate the fpr and tpr for all thresholds of the classification

probs = model.predict_proba(X_test)

preds = probs[:,1]

fpr, tpr, threshold = metrics.roc_curve(y_test, preds)

roc_auc = metrics.auc(fpr, tpr)

# method I: plt

import matplotlib.pyplot as plt

plt.title('Receiver Operating Characteristic')

plt.plot(fpr, tpr, 'b', label = 'AUC = %0.2f' % 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()

# method II: ggplot

from ggplot import *

df = pd.DataFrame(dict(fpr = fpr, tpr = tpr))

ggplot(df, aes(x = 'fpr', y = 'tpr')) + geom_line() + geom_abline(linetype = 'dashed')

或尝试

ggplot(df, aes(x = 'fpr', ymin = 0, ymax = 'tpr')) + geom_line(aes(y =

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值