如何使用Scikit-Learn创建ROC曲线时使用预测分数

nev*_*int 4 python machine-learning roc scikit-learn

我有以下代码:

from sklearn.metrics import roc_curve, auc

actual      = [1,1,1,0,0,1]
prediction_scores = [0.9,0.9,0.9,0.1,0.1,0.1]
false_positive_rate, true_positive_rate, thresholds = roc_curve(actual, prediction_scores, pos_label=1)
roc_auc = auc(false_positive_rate, true_positive_rate)
roc_auc
# 0.875
Run Code Online (Sandbox Code Playgroud)

在该示例中,解释prediction_scores是直截了当的,即得分越高,预测越自信.

现在我有另一组预测预测分数.它是非小数的,而解释则相反.意味着 分数越,预测就越有信心.

prediction_scores_v2 = [10.3,10.3,10.2,10.5,2000.34,2000.34]
# so this is equivalent 
Run Code Online (Sandbox Code Playgroud)

我的问题是:我如何扩展它,prediction_scores_v2以便它像第一个那样给出类似的AUC分数?

换一种方式,Scikit的ROC_CURVE要求y_score积极类的概率估计.如果y_score我有错误等级的概率估计,我 怎么能对待这个值呢?

Tch*_*hke 5

对于AUC,您实际上只关心预测的顺序.因此,只要这是真的,您就可以将预测变为AUC将接受的格式.

您需要除以最大值以使预测值介于0和1之间,然后从1减去,因为在您的情况下,降低更好:

max_pred = max(prediction_scores_v2)
prediction_scores_v2[:] = (1-x/max_pred for x in prediction_scores_v2)

false_positive_rate, true_positive_rate, thresholds = roc_curve(actual, prediction_scores_v2, pos_label=1)
roc_auc = auc(false_positive_rate, true_positive_rate)
# 0.8125
Run Code Online (Sandbox Code Playgroud)