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 08:08:48 浏览: 35
要根据y_score和y_test的对应关系绘制ROC曲线,需要将y_test转换成one-hot编码。假设y_test的shape为(样本数量,),可以使用sklearn库中的OneHotEncoder进行转换:
```
from sklearn.preprocessing import OneHotEncoder
# 将y_test转换成one-hot编码
encoder = OneHotEncoder(sparse=False)
y_test_onehot = encoder.fit_transform(y_test.reshape(-1, 1))
```
然后,可以使用sklearn库中的roc_curve函数计算每个类别的fpr和tpr,并使用matplotlib库绘制ROC曲线:
```
from sklearn.metrics import roc_curve, auc
import matplotlib.pyplot as plt
# 计算每个类别的fpr和tpr以及AUC
n_classes = y_test_onehot.shape[1]
fpr = dict()
tpr = dict()
roc_auc = dict()
for i in range(n_classes):
fpr[i], tpr[i], _ = roc_curve(y_test_onehot[:, i], y_score[:, i])
roc_auc[i] = auc(fpr[i], tpr[i])
# 绘制ROC曲线
plt.figure()
lw = 2
colors = ['red', 'green', 'blue']
for i, color in zip(range(n_classes), colors):
plt.plot(fpr[i], tpr[i], color=color, lw=lw,
label='ROC curve of class {0} (AUC = {1:0.2f})'
''.format(i, roc_auc[i]))
plt.plot([0, 1], [0, 1], color='black', 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')
plt.legend(loc="lower right")
plt.show()
```
其中,y_score的shape为(样本数量, 类别数量)。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)