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对应起来画aoc曲线
时间: 2023-06-28 17:08:46 浏览: 48
首先,由于y_score的shape为(样本数, 类别数),需要将其转化为(样本数,)的形式,即每个样本只有一个得分。可以使用sklearn的roc_curve函数绘制aoc曲线,该函数需要输入两个参数:y_true和y_score,其中y_true为真实标签,y_score为每个样本在每个类别上的得分。因此,需要将y_test转化为one-hot编码的形式,然后取第一个类别的概率作为y_score。
以下是代码示例:
```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((len(y_test), 3))
y_test_onehot[np.arange(len(y_test)), y_test] = 1
# 取第一个类别的概率作为y_score
y_score_ = y_score[:, :, 0].flatten()
# 绘制aoc曲线
fpr, tpr, thresholds = roc_curve(y_test_onehot[:, 0], y_score_)
roc_auc = auc(fpr, tpr)
plt.figure()
plt.plot(fpr, tpr, color='darkorange', lw=2, label='ROC curve (area = %0.2f)' % roc_auc)
plt.plot([0, 1], [0, 1], color='navy', lw=2, 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()
```
其中,y_test为原始的标签,y_score为模型在每个样本上预测的概率分布。需要将其reshape为(样本数, 类别数)的形式。在本例中,假设y_test为(1000,)的向量,y_score为(1000, 3)的矩阵,即1000个样本,每个样本有3个类别的得分。因此,需要对y_score进行如下操作:
```python
y_score = y_score.reshape((len(y_test), 3, -1))
```