如何从具有2个输出神经元的softmax二元分类器绘制ROC曲线?

2024-04-29 05:07:23 发布

您现在位置:Python中文网/ 问答频道 /正文

如何将离散输出标签作为两列绘制roc曲线

使用roc_曲线()会给我一个错误:

ValueError:不支持多标签指示器格式

y_prediction = model.predict(test_X)

y_prediction[1]
Out[27]: array([1.0000000e+00, 6.8178085e-12], dtype=float32)

y_prediction.shape
Out[23]: (514, 2)

test_y.shape
Out[24]: (514, 2)

fpr_roc, tpr_roc, thresholds_roc = roc_curve(test_y, y_prediction)

roc_auc = metrics.auc(fpr_roc, tpr_roc)

Tags: test格式错误绘制标签out曲线指示器
2条回答

由于sklearn中的roc_curve函数只需要正类概率估计,因此可以使用与正类相关的输出维度

例如: preds[:, -1]

根据文档,y_真值和y_分数应为1-d

https://scikit-learn.org/stable/modules/generated/sklearn.metrics.roc_curve.html

y\u truearray,shape=[n\u示例]

因此,只需获取标签,而不是softmax输出

在roc_曲线()之前添加以下行

test_y = np.argmax(test_y, axis=-1) # getting the labels
y_prediction = np.argmax(y_prediction, axis=-1) # getting the confidence of postive class

相关问题 更多 >