【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曲线(二分类)

03-15
### Area Under Precision-Recall Curve (AUPRC) #### 定义 Area Under Precision-Recall Curve (AUPRC) 是一种用于评估二分类模型性能的指标,它表示 Precision-Recall 曲线下的面积。该曲线通过绘制不同阈值下的一系列精度(Precision)召回率(Recall)点来反映模型的表现。 #### 计算方法 AUPRC 的计算基于 Precision Recall 值的变化关系。具体来说: 1. **获取多个阈值下的 Precision Recall** 对于不同的决策阈值 \( t \),可以计算出相应的 Precision Recall 值: \[ \text{Precision} = \frac{\text{True Positives}}{\text{True Positives} + \text{False Positives}} \] \[ \text{Recall} = \frac{\text{True Positives}}{\text{True Positives} + \text{False Negatives}} \] 2. **构建 P-R 曲线** 将上述每一对 \( (\text{Recall}, \text{Precision}) \) 绘制成一条曲线。 3. **计算曲线下面积** 使用数值积分法(如梯形法则),估算 Precision-Recall 曲线下的面积。假设我们有若干个有序的 \( (\text{Recall}_i, \text{Precision}_i) \) 点,则可以通过以下公式近似计算 AUPRC: \[ \text{AUPRC} = \sum_{i=1}^{n-1} \left( \text{Recall}_{i+1} - \text{Recall}_i \right) \cdot \frac{\text{Precision}_{i+1} + \text{Precision}_i}{2} \] 这里每一项对应的是相邻两点间形成的梯形面积[^1]。 #### Python 实现示例 以下是利用 `scikit-learn` 库实现 AUPRC 的代码片段: ```python from sklearn.metrics import precision_recall_curve, auc import numpy as np # 示例真实标签预测概率 y_true = np.array([0, 0, 1, 1]) y_scores = np.array([0.1, 0.4, 0.35, 0.8]) # 获取 Precision Recall 数组 precision, recall, _ = precision_recall_curve(y_true, y_scores) # 计算 AUPRC auprc_value = auc(recall, precision) print(f"AUPRC Value: {auprc_value}") ``` 此代码首先调用函数生成 Precision Recall 数据序列,随后应用 `auc()` 函数完成实际求积操作[^2]。 #### 特殊情况处理 当正样本数量较少或者分布极不均衡时,可能会导致某些区间的 Precision 或者 Recall 变化剧烈,此时需特别注意插值方式的选择以及边界条件的影响[^3]。 ---
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值