y_score[[[0.10579571 0.89420429] [0.55098666 0.44901334] [0.36715617 0.63284383] ... [0.80762724 0.19237276] [0.6495282 0.3504718 ] [0.64156421 0.35843579]] [[0.9323137 0.0676863 ] [0.5349633 0.4650367 ] [0.75478328 0.24521672] ... [0.27653182 0.72346818] [0.4277243 0.5722757 ] [0.44331892 0.55668108]] [[0.96189059 0.03810941] [0.91405004 0.08594996] [0.87806054 0.12193946] ... [0.91584095 0.08415905] [0.9227475 0.0772525 ] [0.91511687 0.08488313]]]怎么和三分类的y_test对应得到roc曲线
时间: 2023-06-28 09:08:49 浏览: 53
首先需要将y_test转换为one-hot编码,然后使用sklearn.metrics库中的roc_curve函数计算每个类别的ROC曲线,并绘制出来。
以下是示例代码:
```python
import numpy as np
from sklearn.metrics import roc_curve, auc
import matplotlib.pyplot as plt
# 将y_test转换为one-hot编码
y_test_onehot = np.zeros((y_test.shape[0], 3))
y_test_onehot[np.arange(y_test.shape[0]), y_test] = 1
# 计算每个类别的ROC曲线
fpr = dict()
tpr = dict()
roc_auc = dict()
for i in range(3):
fpr[i], tpr[i], _ = roc_curve(y_test_onehot[:, i], y_score[:, i, 1])
roc_auc[i] = auc(fpr[i], tpr[i])
# 绘制ROC曲线
plt.figure()
lw = 2
colors = ['red', 'green', 'blue']
for i, color in zip(range(3), colors):
plt.plot(fpr[i], tpr[i], color=color, lw=lw,
label='ROC curve of class {0} (area = {1:0.2f})'
''.format(i, roc_auc[i]))
plt.plot([0, 1], [0, 1], 'k--', lw=lw)
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')
plt.legend(loc="lower right")
plt.show()
```
其中,y_test是三分类的标签,y_score是模型的预测结果,形状为(样本数, 类别数, 类别对应的概率)。在上面的代码中,我们将y_test转换为one-hot编码,然后分别计算了每个类别的ROC曲线,并绘制出来。
阅读全文