该问题与以下链接中提到的问题类似,请仔细阅读以供参考。
How does sklearn calculate the area under the roc curve for two binary inputs?
我知道sklearn.metrics._binary_clf_curve
中发生的一切。
但是对于二进制分类,如何在所述函数中计算/确定多个阈值。该函数返回y_score[threshold_idxs]
作为绘制roc_curve的阈值,我无法理解y_score[threshold_idxs]
的计算以及为什么将其作为阈值。
答案 0 :(得分:3)
让我们以scikit-learn 0.22.2 documentation作为指南针来了解该函数的每个组件以及最终结果。
sklearn.metrics.roc_curve(y_true, y_score, pos_label=None, sample_weight=None, drop_intermediate=True)
“活动” 参数(如果使用默认调用):
y_true
:数组,形状= [n_samples],真二进制标签。y_score
:数组,形状= [n_samples]。目标分数可以是肯定类别的概率估计值,置信度值或决策的非阈值度量drop_intermediate
:布尔值,可选(默认= 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]
之后,您可以看到其他操作,但是核心是我上面强调的内容。