繁体   English   中英

计算二元分类的 roc_curve 阈值

[英]Calculate threshold for roc_curve for binary classification

该问题与以下链接中提到的问题相似,请阅读以供参考。

sklearn如何计算两个二进制输入的roc曲线下的面积?

我知道一切都在sklearn.metrics._binary_clf_curve发生。

但是对于二元分类,如何在所述函数中计算/决定多个阈值。 该函数返回y_score[threshold_idxs]作为绘制 roc_curve 的阈值,我无法理解y_score[threshold_idxs]的计算以及为什么这会是阈值。

让我们使用scikit-learn 0.22.2 文档作为指南针来了解函数的每个组件和最终结果。

  • 功能
sklearn.metrics.roc_curve(y_true, y_score, pos_label=None, sample_weight=None, drop_intermediate=True)
  • 如果使用默认调用,则为“active”参数

    • y_true :数组,形状 = [n_samples],真正的二进制标签。
    • y_score :数组,形状 = [n_samples]。 目标分数,可以是正类的概率估计、置信度值或决策的非阈值度量
    • drop_intermediate : boolean, optional (default=True), 是否删除一些不会出现在绘制的 ROC 曲线上的次优阈值。
  • 输出

    • fpr : 数组,形状 = [>2],增加误报率,使得元素 i 是得分 >= 阈值 [i] 的预测的误报率。
    • tpr : 数组,形状 = [>2],增加真阳性率,使得元素 i 是得分 >= 阈值 [i] 的预测的真阳性率。
    • thresholds :数组,形状 = [n_thresholds],降低用于计算 fpr 和 tpr 的决策函数的阈值

现在,考虑roc_curve()的代码,它调用函数_binary_clf_curve() ,在适当的操作和排序之后,它计算:

distinct_value_indices = np.where(np.diff(y_score))[0]
threshold_idxs = np.r_[distinct_value_indices, y_true.size - 1]

这些行的解释在评论中:

y_score通常有许多绑定值。 在这里,我们提取与不同值相关的索引。 我们还连接了曲线末端的值。

上面的两行粗略地回答了您的问题如何计算/决定多个阈值

然后,它计算:

tps = stable_cumsum(y_true * weight)[threshold_idxs]
fps = 1 + threshold_idxs - tps

并返回:

return fps, tps, y_score[threshold_idxs]

之后,返回主函数roc_curve()if drop_intermediate and len(fps) > 2: ,则

尝试降低与中间点相对应的阈值,并与其他点共线。

optimal_idxs = np.where(np.r_[True,
                              np.logical_or(np.diff(fps, 2),
                                            np.diff(tps, 2)),
                              True])[0]

“新”值是:

fps = fps[optimal_idxs]
tps = tps[optimal_idxs]
thresholds = thresholds[optimal_idxs]

之后您可以看到其他操作,但核心是我在上面强调的内容。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM