scikit-learn roc_curve:为什么它会在一段时间内返回一个阈值= 2?

Blu*_*eet 9 roc scikit-learn

如果我错了,请纠正我:scikit-learn的roc_curve返回的"阈值"应该是[0,1]中的数字数组.但是,它有时会给我一个第一个数字接近"2"的数组.这是一个错误还是我做错了?谢谢.

In [1]: import numpy as np

In [2]: from sklearn.metrics import roc_curve

In [3]: np.random.seed(11)

In [4]: aa = np.random.choice([True, False],100)

In [5]: bb = np.random.uniform(0,1,100)

In [6]: fpr,tpr,thresholds = roc_curve(aa,bb)

In [7]: thresholds
Out[7]: 
array([ 1.97396826,  0.97396826,  0.9711752 ,  0.95996265,  0.95744405,
    0.94983331,  0.93290463,  0.93241372,  0.93214862,  0.93076592,
    0.92960511,  0.92245024,  0.91179548,  0.91112166,  0.87529458,
    0.84493853,  0.84068543,  0.83303741,  0.82565223,  0.81096657,
    0.80656679,  0.79387241,  0.77054807,  0.76763223,  0.7644911 ,
    0.75964947,  0.73995152,  0.73825262,  0.73466772,  0.73421299,
    0.73282534,  0.72391126,  0.71296292,  0.70930102,  0.70116428,
    0.69606617,  0.65869235,  0.65670881,  0.65261474,  0.6487222 ,
    0.64805644,  0.64221486,  0.62699782,  0.62522484,  0.62283401,
    0.61601839,  0.611632  ,  0.59548669,  0.57555854,  0.56828967,
    0.55652111,  0.55063947,  0.53885029,  0.53369398,  0.52157349,
    0.51900774,  0.50547317,  0.49749635,  0.493913  ,  0.46154029,
    0.45275916,  0.44777116,  0.43822067,  0.43795921,  0.43624093,
    0.42039077,  0.41866343,  0.41550367,  0.40032843,  0.36761763,
    0.36642721,  0.36567017,  0.36148354,  0.35843793,  0.34371331,
    0.33436415,  0.33408289,  0.33387442,  0.31887024,  0.31818719,
    0.31367915,  0.30216469,  0.30097917,  0.29995201,  0.28604467,
    0.26930354,  0.2383461 ,  0.22803687,  0.21800338,  0.19301808,
    0.16902881,  0.1688173 ,  0.14491946,  0.13648451,  0.12704826,
    0.09141459,  0.08569481,  0.07500199,  0.06288762,  0.02073298,
    0.01934336])
Run Code Online (Sandbox Code Playgroud)

joe*_*eln 6

大多数情况下,不使用这些阈值,例如计算曲线下面积,或绘制假阳性率与真阳性率.

然而,为了绘制看似合理的曲线,需要有一个包含0个数据点的阈值.由于Scikit-Learn的ROC曲线函数不需要对阈值进行归一化概率(任何得分都很好),因此将此点的阈值设置为1是不够的; 设置它inf是明智的,但编码器通常期望有限的数据(并且实现也可能适用于整数阈值).相反,实现使用max(score) + epsilonwhere epsilon = 1.这可能是美容缺陷,但你没有说明为什么这是一个问题!


afr*_*iro 6

从文档:

阈值:数组,形状 = [n_thresholds] 降低用于计算 fpr 和 tpr 的决策函数的阈值。thresholds[0]表示没有被预测的实例并被任意设置为max(y_score) + 1

因此, 的第一个元素thresholds接近 2 因为它是max(y_score) + 1,在您的情况下thresholds[1] + 1


Pet*_*fer 1

这对我来说似乎是一个错误 - 在 roc_curve(aa,bb) 中,1 被添加到第一个阈值。您应该在此处创建一个问题https://github.com/scikit-learn/scikit-learn/issues