pytorch 多分类问题,计算百分比操作

作者:风泽茹岚 时间:2023-01-07 22:57:41 

二分类或分类问题,网络输出为二维矩阵:批次x几分类,最大的为当前分类,标签为one-hot型的二维矩阵:批次x几分类

计算百分比有numpy和pytorch两种实现方案实现,都是根据索引计算百分比,以下为具体二分类实现过程。

pytorch


out = torch.Tensor([[0,3],
    [2,3],
    [1,0],
    [3,4]])
cond = torch.Tensor([[1,0],
     [0,1],
     [1,0],
     [1,0]])

persent = torch.mean(torch.eq(torch.argmax(out, dim=1), torch.argmax(cond, dim=1)).double())
print(persent)

numpy


out = [[0, 3],
 [2, 3],
 [1, 0],
 [3, 4]]
cond = [[1, 0],
 [0, 1],
 [1, 0],
 [1, 0]]
a = np.argmax(out,axis=1)
b = np.argmax(cond, axis=1)
persent = np.mean(np.equal(a, b) + 0)
# persent = np.mean(a==b + 0)
print(persent)

补充知识:python 多分类画auc曲线和macro-average ROC curve

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


IMAGE y_real y_predict 0其他 1豹纹 2弥漫 3斑片 4黄斑
/mnt/AI/HM/izy20200531c5/299/train/0其他/IM005111 (Copy).jpg 0 0 1 8.31E-19 7.59E-13 4.47E-15 2.46E-14
/mnt/AI/HM/izy20200531c5/299/train/0其他/IM005201 (Copy).jpg 0 0 1 5.35E-17 4.38E-11 8.80E-13 3.85E-11
/mnt/AI/HM/izy20200531c5/299/train/0其他/IM004938 (4) (Copy).jpg 0 0 1 1.20E-16 3.17E-11 6.26E-12 1.02E-11
/mnt/AI/HM/izy20200531c5/299/train/0其他/IM004349 (3) (Copy).jpg 0 0 1 5.66E-14 1.87E-09 6.50E-09 3.29E-09
/mnt/AI/HM/izy20200531c5/299/train/0其他/IM004673 (5) (Copy).jpg 0 0 1 5.51E-17 9.30E-12 1.33E-13 2.54E-12
/mnt/AI/HM/izy20200531c5/299/train/0其他/IM004450 (5) (Copy).jpg 0 0 1 4.81E-17 3.75E-12 3.96E-13 6.17E-13

导入基础的pandas和keras处理函数

import pandas as pd

from keras.utils import to_categorical

导入数据

data=pd.read_excel('5分类新.xlsx')

data.head()

导入机器学习库


from sklearn.metrics import precision_recall_curve
import numpy as np
from matplotlib import pyplot
from sklearn.metrics import f1_score
from 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和tpr


n_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 auc


from scipy import interp
# First aggregate all false positive rates
all_fpr = np.unique(np.concatenate([fpr[i] for i in range(n_classes)]))

# Then interpolate all ROC curves at this points
mean_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 AUC
mean_tpr /= n_classes

fpr["macro"] = all_fpr
tpr["macro"] = mean_tpr
roc_auc["macro"] = auc(fpr["macro"], tpr["macro"])

画图


import matplotlib.pyplot as plt
from itertools import cycle
from matplotlib.ticker import FuncFormatter
lw = 2
# Plot all ROC curves
plt.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()

展示

pytorch 多分类问题,计算百分比操作

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

来源:https://blog.csdn.net/luolinll1212/article/details/83897047

标签:pytorch,多分类,百分比
0
投稿

猜你喜欢

  • 月影:function扩展

    2008-05-19 12:27:00
  • 简单了解django缓存方式及配置

    2022-11-20 05:04:17
  • php !function_exists("T7FC56270E7A70FA81A5935B72EACBE29"))代码解密

    2023-11-21 14:36:02
  • php header功能的使用

    2023-11-15 09:25:26
  • Python 敏感词过滤的实现示例

    2021-07-04 12:17:28
  • Python对图片进行resize、裁剪、旋转、翻转问题

    2023-03-01 11:07:05
  • JavaScript画圆

    2010-01-22 15:57:00
  • Pytorch maxpool的ceil_mode用法

    2023-03-20 13:28:05
  • 详解php中的类与对象(继承)

    2023-11-23 14:07:09
  • javascript过滤数组重复元素的实现方法

    2023-09-08 00:41:21
  • Python读取ini配置文件传参的简单示例

    2022-02-06 09:51:36
  • 解读MaxPooling1D和GlobalMaxPooling1D的区别

    2023-07-21 10:54:43
  • 用python修改excel表某一列内容的操作方法

    2022-01-22 20:51:29
  • python类的方法属性与方法属性的动态绑定代码详解

    2023-07-02 03:31:26
  • 网页设计标准尺寸参考

    2007-12-29 20:42:00
  • ASP和MYSQL开发网站的注意事项

    2009-08-21 13:23:00
  • python-opencv中的cv2.inRange函数用法说明

    2022-09-29 23:39:08
  • Oracle 安装和卸载问题收集(集合篇)第1/6页

    2009-07-02 12:20:00
  • Python基于Django实现验证码登录功能

    2023-06-25 03:21:13
  • Python爬虫框架NewSpaper使用详解

    2022-06-03 06:20:26
  • asp之家 网络编程 m.aspxhome.com