ROC AUC值为0

问题描述 投票:2回答:1

我已经训练了一个二元分类器,但我认为我的ROC曲线不正确。

这是包含标签的向量:

y_true= [0, 1, 1, 1, 0, 1, 0, 1, 0]

第二个向量是得分向量

y_score= [
    0.43031937, 0.09115553, 0.00650781, 0.02242869, 0.38608587, 
    0.09407699, 0.40521139, 0.08062053, 0.37445426
]

当我绘制我的ROC曲线时,我得到以下结果:

enter image description here

我认为代码是正确的,但我不明白为什么我得到这条曲线以及为什么tprfprthreshold列表的长度为4.为什么我的AUC等于零?

fpr [0.   0.25 1.   1.  ]
tpr [0. 0. 0. 1.]
thershold [1.43031937 0.43031937 0.37445426 0.00650781]

我的代码:

import sklearn.metrics as metrics

fpr, tpr, threshold = metrics.roc_curve(y_true, y_score)
roc_auc = metrics.auc(fpr, tpr)

# method I: plt
import matplotlib.pyplot as plt
plt.title('Receiver Operating Characteristic')
plt.plot(fpr, tpr, 'b', label = 'AUC = %0.2f' % roc_auc)
plt.legend(loc = 'lower right')
plt.plot([0, 1], [0, 1],'r--')
plt.xlim([0, 1])
plt.ylim([0, 1])
plt.ylabel('True Positive Rate')
plt.xlabel('False Positive Rate')
plt.show()
python python-3.x scikit-learn roc
1个回答
2
投票

关于AUC要记住的一件事是,真正重要的是距离0.5。如果你的AUC非常低,那就意味着你的“正面”和“负面”标签都会被切换。

看看你的分数,很明显低分(任何小于~0.095)意味着1,高于该阈值的任何东西都是0.所以你实际上有一个很棒的二元分类器!

问题在于,默认情况下,较高的分数与标签1相关联。因此,您将高分的分数标记为1而不是0。因此,你100%的时间都是错的。在这种情况下,只需切换您的预测,您就会100%正确。

简单的解决方法是使用pos_label参数来sklearn.metrics.roc_curve。在这种情况下,您希望您的正面标签为0。

fpr, tpr, threshold = metrics.roc_curve(y_true, y_score, pos_label=0)
roc_auc = metrics.auc(fpr, tpr)
print(roc_auc)
#1.0
© www.soinside.com 2019 - 2024. All rights reserved.