首页 > 编程知识 正文

python 多分类画auc曲线和macroaverage ROC curve,多分类roc曲线意义

时间:2023-05-03 17:20:46 阅读:266052 作者:2535

最近帮一个人做了一个多分类画auc曲线的东西,不过最后那个人不要了,还被说了一顿,心里很是不爽,anyway,我写代码的还是要继续写代码的,所以我准备把我修改的代码分享开来,供大家研究学习。处理的数据大改是这种xlsx文件:

IMAGE y_real y_predict 0其他 1豹纹 2弥漫 3斑片 4黄斑/mnt/AI/HM/izy20200531c5/299/train/0其他/IM005111 (Copy).jpg0018.31E-197.59E-134.47E-152.46E-14/mnt/AI极速赛车7码口诀

最近帮一个人做了一个多分类画auc曲线的东西,不过最后那个人不要了,还被说了一顿,心里很是不爽,anyway,我写代码的还是要继续写代码的,所以我准备把我修改的代码分享开来,供大家研究学习。处理的数据大改是这种xlsx文件:

IMAGE y_real y_predict 0其他 1豹纹 2弥漫 3斑片 4黄斑/mnt/AI/HM/izy20200531c5/299/train/0其他/IM005111 (Copy).jpg0018.31E-197.59E-134.47E-152.46E-14/mnt/AI/HM/izy20200531c5/299/train/0其他/IM005201 (Copy).jpg0015.35E-174.38E-118.80E-133.85E-11/mnt/AI/HM/izy20200531c5/299/train/0其他/IM004938 (4) (Copy).jpg0011.20E-163.17E-116.26E-121.02E-11/mnt/AI/HM/izy20200531c5/299/train/0其他/IM004349 (3) (Copy).jpg0015.66E-141.87E-096.50E-093.29E-09/mnt/AI/HM/izy20200531c5/299/train/0其他/IM004673 (5) (Copy).jpg0015.51E-179.30E-121.33E-132.54E-12/mnt/AI/HM/izy20200531c5/299/train/0其他/IM004450 (5) (Copy).jpg0014.81E-173.75E-123.96E-136.17E-13

 

导入基础的pandas和keras处理函数import pandas as pdfrom keras.utils import to_categorical 导入数据data=pd.read_excel('5分类新.xlsx')data.head() 导入机器学习库from sklearn.metrics import precision_recall_curveimport numpy as npfrom matplotlib import pyplotfrom sklearn.metrics import f1_scorefrom sklearn.metrics import roc_curve, auc 把ground truth提取出来true_y=data[' y_real'].to_numpy()true_y=to_categorical(true_y) 把每个类别的数据提取出来PM_y=data[[' 0其他',' 1豹纹',' 2弥漫',' 3斑片',' 4黄斑']].to_numpy()PM_y.shape 计算每个类别的fpr和tprn_classes=PM_y.shape[1]fpr = dict()tpr = dict()roc_auc = dict()for i in range(n_classes): fpr[i], tpr[i], _ = roc_curve(true_y[:, i], PM_y[:, i]) roc_auc[i] = auc(fpr[i], tpr[i]) 计算macro aucfrom scipy import interp# First aggregate all false positive ratesall_fpr = np.unique(np.concatenate([fpr[i] for i in range(n_classes)]))# Then interpolate all ROC curves at this pointsmean_tpr = np.zeros_like(all_fpr)for i in range(n_classes): mean_tpr += interp(all_fpr, fpr[i], tpr[i])# Finally average it and compute AUCmean_tpr /= n_classesfpr["macro"] = all_fprtpr["macro"] = mean_tprroc_auc["macro"] = auc(fpr["macro"], tpr["macro"]) 画图import matplotlib.pyplot as pltfrom itertools import cyclefrom matplotlib.ticker import FuncFormatterlw = 2# Plot all ROC curvesplt.figure()labels=['Category 0','Category 1','Category 2','Category 3','Category 4']plt.plot(fpr["macro"], tpr["macro"], label='macro-average ROC curve (area = {0:0.4f})' ''.format(roc_auc["macro"]), color='navy', linestyle=':', linewidth=4)colors = cycle(['aqua', 'darkorange', 'cornflowerblue','blue','yellow'])for i, color in zip(range(n_classes), colors): plt.plot(fpr[i], tpr[i], color=color, lw=lw, label=labels[i]+'(area = {0:0.4f})'.format(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('1-Specificity (%)')plt.ylabel('Sensitivity (%)')plt.title('Some extension of Receiver operating characteristic to multi-class')def to_percent(temp, position): return '%1.0f'%(100*temp)plt.gca().yaxis.set_major_formatter(FuncFormatter(to_percent))plt.gca().xaxis.set_major_formatter(FuncFormatter(to_percent))plt.legend(loc="lower right")plt.show()

展示

上述的代码是在jupyter中运行的,所以是分开的

参考文献

[1].ROC原理介绍及利用python实现二分类和多分类的ROC曲线. https://blog.csdn.net/xyz1584172808/article/details/81839230

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。