Python sklearn ROC-AUC曲线只有一个特征和各种阈值

Python sklearn ROC-AUC curve with only one feature and various thresholds

我在这个领域相对较新,现在有点困惑...我将解释一下:我的数据中有一些元素,每个元素的值都在 0 到 1 之间,并且有一个关联的标签 (1 , 0).我需要测试一些阈值,例如阈值 = 0.4,所有 > 0.4 的值都将被预测为真 (1),所有 < 0.4 的值都将被预测为假 (0)。我认为我不需要机器学习分类器,因为根据我选择的阈值,我已经知道分配给每个元素的标签。

这是我到目前为止所做的:

prediction = []
for row in range(dfAggr.shape[0]):
    if dfAggr['value'].values[row] >= threshold:
        prediction.append(1)
    else
        prediction.append(0)

label = dfAggr['truth'].values.astype(int)

#ROC CURVE
fpr, tpr, thresholds = roc_curve(label, prediction)
roc_auc = auc(fpr, tpr)
plt.plot(fpr, tpr, lw=1, label='ROC (area = %0.2f)' % (roc_auc))
plt.plot([0, 1], [0, 1], '--', color=(0.6, 0.6, 0.6), label='Luck')
plt.xlim([-0.05, 1.05])
plt.ylim([-0.05, 1.05])
plt.grid()
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('Receiver Operating Characteristic')
plt.legend(loc="lower right")
plt.savefig("rocauc.pdf", format="pdf")
plt.show()

我得到了这个情节:

我认为这个图是完全错误的,因为我想通过测试 0 到 1 之间的每个可能阈值来构建 ROC 曲线,以获得最佳截止值。

我所做的在概念上是错误的吗?

我假设您正在使用 from sklearn.metrics import roc_curveroc_curve 函数会为您完成所有的阈值,您无需 pre-select 自己。

你应该这样做:

predictions =  dfAggr['value'].values
label = dfAggr['truth'].values.astype(int)
fpr, tpr, thresholds = roc_curve(label, predictions)
[...]