我正在尝试为多类预测(虹膜数据集-3类)问题重现ROC曲线。如果测试数据集和测试预测为三维数组格式,则sklearn
软件包中的This script可以正常工作。如何使用以下格式绘制相同的ROC曲线?
y_test
array([0, 2, 2, 0, 1, 2, 1, 2, 1, 2, 2, 1, 1, 0, 2, 2, 2, 2, 1, 2, 0, 0,
2, 0, 0, 1, 1, 0, 0, 2, 0, 2, 1, 0, 1, 1, 0, 1, 1, 1, 2, 0, 2, 2,
2, 2, 1, 1, 1, 2, 1, 2, 1, 1, 2, 2, 0, 1, 2, 1, 1, 2, 1, 2, 0, 1,
2, 2, 0, 2, 2, 0, 1, 0, 0, 2, 2, 2, 0, 0, 0, 2, 1, 2, 1, 1, 1, 0,
1, 2, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 2, 2, 1, 2, 2, 0, 1,
1, 2, 2, 0, 2, 1, 0, 0, 1, 2])
y_preds
tensor([0, 2, 1, 0, 1, 2, 1, 2, 1, 2, 2, 1, 1, 0, 2, 2, 2, 2, 1, 2, 0, 0, 2, 0,
0, 1, 1, 0, 0, 2, 0, 2, 1, 0, 1, 1, 0, 1, 1, 1, 2, 0, 2, 2, 2, 2, 1, 1,
1, 2, 1, 2, 1, 1, 2, 2, 0, 1, 2, 2, 1, 2, 1, 2, 0, 1, 2, 2, 0, 2, 2, 0,
1, 0, 0, 2, 2, 2, 0, 0, 0, 1, 1, 2, 1, 2, 2, 0, 1, 2, 0, 0, 0, 0, 0, 1,
0, 0, 1, 1, 1, 1, 0, 2, 2, 1, 2, 2, 0, 1, 1, 2, 2, 0, 2, 1, 0, 0, 1, 2])
我尝试过的代码
from sklearn import metrics
fpr, tpr, thresholds = metrics.roc_curve(y_test, y_score, pos_label=2)
roc_auc = auc(fpr, tpr)
plt.figure()
lw = 2
plt.plot(fpr, tpr, color='darkorange',
lw=lw, label='ROC curve (area = %0.2f)' % roc_auc)
plt.plot([0, 1], [0, 1], color='navy', lw=lw, linestyle='--')
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('Receiver operating characteristic example')
plt.legend(loc="lower right")
plt.show()