【Python绘图】Python绘制roc和aupr曲线

本文介绍了如何使用Python的`sklearn.metrics`库绘制AUC ROC曲线和AUPR曲线,展示二分类模型的性能。通过示例代码详细解释了绘制过程,并展示了绘制出的曲线结果。同时,提供了计算AUC和AUPR的步骤,帮助理解这些关键性能指标。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


  在进行模型方法性能对比的时候,往往需要将自己的方法和 b a s e l i n e   m o d e l baseline~model baseline model的性能指标绘制绘制到同一个 F i g u r e Figure Figure进行对比,如Area Under the Receiver Operating Characteristic (AUROC)。具体的绘制方法如下(Python script, 以二分类为例):

1 绘制AUC曲线(绘制多个曲线到同一张图中)

from sklearn import metrics
import matplotlib.pylab as plt

plt.rc('font', family='Times New Roman')

y_true_1 = [1.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0,
            0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0]

y_score_2 = [0.99, 0.98, 0.97, 0.93, 0.85, 0.80, 0.79, 0.75, 0.70, 0.65,
             0.64, 0.63, 0.55, 0.54, 0.51, 0.49, 0.30, 0.2, 0.1, 0.09]

fpr1, tpr1, thresholds = metrics.roc_curve(y_true_1, y_score_2)
roc_auc1 = metrics.auc(fpr1, tpr1)  # the value of roc_auc1
print(roc_auc1)
plt.plot(fpr1, tpr1, 'b', label='AUC = %0.2f' % roc_auc1)

# ++++++++++++++++++++++++++++++++++++++++++++++++++++++

y_true_2 = [0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 1.0,
            0.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 1.0, 0.0]

y_score_2 = [0.43, 0.56, 0.97, 0.76, 0.85, 0.85, 0.79, 0.75, 0.60, 0.65,
             0.64, 0.80, 0.40, 0.54, 0.90, 0.49, 0.30, 0.2, 0.9, 0.45]

fpr2, tpr2, _ = metrics.roc_curve(y_true_2, y_score_2)
roc_auc2 = metrics.auc(fpr2, tpr2)  # the value of roc_auc1
print(roc_auc2)
plt.plot(fpr2, tpr2, 'r', label='AUC = %0.2f' % roc_auc2)

plt.legend(loc='lower right')
plt.plot([0, 1], [0, 1], 'r--')
# plt.xlim([0, 1])  # the range of x-axis
# plt.ylim([0, 1])  # the range of y-axis
plt.xlabel('False Positive Rate')  # the name of x-axis
plt.ylabel('True Positive Rate')  # the name of y-axis
plt.title('Receiver operating characteristic example')  # the title of figure
plt.show()

2 AUC曲线结果

在这里插入图片描述

3 绘制AUPR曲线

from sklearn import metrics
import matplotlib.pylab as plt

plt.rc('font', family='Times New Roman')

y_true_1 = [1.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0,
            0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0]

y_score_2 = [0.99, 0.98, 0.97, 0.93, 0.85, 0.80, 0.79, 0.75, 0.70, 0.65,
             0.64, 0.63, 0.55, 0.54, 0.51, 0.49, 0.30, 0.2, 0.1, 0.09]

precision1, recall1, _ = metrics.precision_recall_curve(y_true_1, y_score_2)
aupr1 = metrics.auc(recall1, precision1)  # the value of roc_auc1
print(aupr1)
plt.plot(recall1, precision1, 'b', label='AUC = %0.2f' % aupr1)

# ++++++++++++++++++++++++++++++++++++++++++++++++++++++
y_true_2 = [1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0]
y_score_2 = [0.9, 0.75, 0.86, 0.47, 0.55, 0.56, 0.74, 0.62, 0.5, 0.86,
            0.8, 0.47, 0.44, 0.67, 0.43, 0.4, 0.52, 0.4, 0.35, 0.1]


precision2, reacall2, _ = metrics.precision_recall_curve(y_true_2, y_score_2)
aupr2 = metrics.auc(reacall2, precision2)  # the value of roc_auc1
print(aupr2)
plt.plot(reacall2, precision2, 'r', label='AUC = %0.2f' % aupr2)

plt.legend(loc='lower left')
plt.plot([1, 0], 'r--', color='b')
# plt.xlim([0, 1])  # the range of x-axis
# plt.ylim([0, 1])  # the range of y-axis
plt.xlabel('Recall')  # the name of x-axis
plt.ylabel('Precision')  # the name of y-axis
plt.title('Precision-Recall')  # the title of figure
plt.show()

4 AUPR结果

在这里插入图片描述

5 参考文献

[1]如何在一张图中画多条ROC线?
[2]如何画ROC曲线和FROC曲线
[4]Python3绘制P-R曲线(二分类)

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值