【发布时间】:2020-12-22 10:38:00
【问题描述】:
我想为我的分类模型绘制 ROC 曲线。由于我是新手,所以我阅读了它,看到了一些帖子并参考了this SO answer 我创建了一个 roc 曲线。
我的数据是这样的:
print(Y.shape)
print(predictions.shape)
print(Y)
print(predictions)
(1, 400)
(1, 400)
[[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1]]
[[0 1 1 1 0 1 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 1 0 1 1 1 0 1 1 1 1 0 1 1 1 0 1 0 0 1 1 1 1 1 1 0 0
1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 0 1
0 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 0 1 1 1
1 1 1 1 1 0 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 0 1 0
0 0 1 0]]
实现代码后:
from sklearn.metrics import precision_score
print('Precsion score: '+ str(precision_score(Y.ravel(), predictions.ravel())))
from sklearn.metrics import recall_score
print('Recall score: '+ str(recall_score(Y.ravel(), predictions.ravel())))
from sklearn.metrics import f1_score
print('F1 score: '+ str(f1_score(Y.ravel(), predictions.ravel())))
from sklearn.metrics import roc_auc_score, auc, roc_curve
print('ROC score: ' + str(roc_auc_score(Y.ravel(), predictions.ravel())))
from sklearn.metrics import confusion_matrix
print('Confusion matrix: ')
print(confusion_matrix(Y.ravel(), predictions.ravel()))
fpr = dict()
tpr = dict()
threshold = dict()
roc_auc = dict()
for i in range(2):
fpr[i], tpr[i], threshold[i] = roc_curve(Y.ravel(), predictions.ravel())
roc_auc[i] = auc(fpr[i], tpr[i])
print(fpr, tpr, threshold, roc_auc)
plt.figure()
plt.plot(fpr[1], tpr[1])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('Receiver operating characteristic(ROC Curve)')
输出:
Precsion score: 0.9179487179487179
Recall score: 0.895
F1 score: 0.9063291139240507
ROC score: 0.9075
Confusion matrix:
[[184 16]
[ 21 179]]
{0: array([0. , 0.08, 1. ]), 1: array([0. , 0.08, 1. ])} {0: array([0. , 0.895, 1. ]), 1: array([0. , 0.895, 1. ])} {0: array([2, 1, 0]), 1: array([2, 1, 0])} {0: 0.9075, 1: 0.9075}
Text(0.5, 1.0, 'Receiver operating characteristic(ROC Curve)')
我不明白为什么要使用循环?我可以看到 FPR、TPR、Threshold 和 roc_auc 的每一行都计算了三个值。我确实读过 roc_curve 将概率作为目标分数(我会研究这个)。但是,我无法从输入的 (1,400) 维数据了解这些数组是如何计算的?
提前致谢。
【问题讨论】:
-
正如链接的 SO 答案清楚地表明,您需要概率预测才能绘制 ROC 曲线,不是像您在这里所做的那样 0/1 硬类。
标签: python numpy machine-learning scikit-learn roc