【问题标题】:Scikit - How to define thresholds for plotting roc curveScikit - 如何定义绘制 roc 曲线的阈值
【发布时间】:2018-07-17 08:39:46
【问题描述】:

我有一个提升树模型以及测试数据集的概率和分类。我正在尝试绘制相同的 roc_curve。但我无法弄清楚如何在 scikit learn 中定义 roc 曲线的阈值/alpha。

from sklearn.metrics import precision_recall_curve,roc_curve,auc, average_precision_score

fpr = dict()
tpr = dict()
roc_auc = dict()

fpr,tpr,_ = roc_curve(ytest,p_test, pos_label=1)
roc_auc = auc(fpr,tpr)

plt.figure()
lw = 2
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('ROCProb.png')
plt.show()

我在这里看到了一个类似的问题:thresholds in roc_curve in scikit learn

但是想不通。我也愿意使用其他一些库。

【问题讨论】:

  • fpr 和 tpr 中的每个值都是针对某个阈值计算的,这些阈值的值在第三个输出 roc_curve 中返回(在您的情况下为变量 _)
  • @sgDysregulation - 谢谢!请张贴作为答案。这也会对其他人有所帮助!
  • @Taurn 不客气,完成。
  • 虽然这并没有真正回答这个问题,但也许 OP 很高兴 - 问题询问如何定义阈值,而不是 roc_curve 计算它们是什么?我想知道后者的答案! @Dreamsn?

标签: python scikit-learn roc


【解决方案1】:

fprtpr 中的每个值都是针对某个阈值计算的,这些阈值的值在第三个输出 roc_curve(在您的情况下为变量 _)中返回

这是一个例子

import numpy as np
from sklearn import metrics
y_true = np.array([1, 1, 2, 2])
y_scores = np.array([0.1, 0.4, 0.35, 0.8])
fpr, tpr, thresholds = metrics.roc_curve(y_true, y_scores, pos_label=2)

将要演示的数据制成表格

   Threshold  FPR  TPR
0       0.80  0.0  0.5
1       0.40  0.5  0.5
2       0.35  0.5  1.0
3       0.10  1.0  1.0

上面的第一行显示,对于阈值 0.8,fpr 为 0,tpr 为 0.5,依此类推

【讨论】:

    猜你喜欢
    • 2019-04-19
    • 1970-01-01
    • 2016-08-24
    • 2018-12-28
    • 1970-01-01
    • 2019-03-10
    • 2013-04-27
    • 2015-06-23
    • 2016-02-04
    相关资源
    最近更新 更多