欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

pytorch 多分類問題,計算百分比操作

 更新時間:2020年07月09日 09:49:01   作者:風(fēng)澤茹嵐  
這篇文章主要介紹了pytorch 多分類問題,計算百分比操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

二分類或分類問題,網(wǎng)絡(luò)輸出為二維矩陣:批次x幾分類,最大的為當(dāng)前分類,標簽為one-hot型的二維矩陣:批次x幾分類

計算百分比有numpy和pytorch兩種實現(xiàn)方案實現(xiàn),都是根據(jù)索引計算百分比,以下為具體二分類實現(xiàn)過程。

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,我寫代碼的還是要繼續(xù)寫代碼的,所以我準備把我修改的代碼分享開來,供大家研究學(xué)習(xí)。處理的數(shù)據(jù)大改是這種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

導(dǎo)入基礎(chǔ)的pandas和keras處理函數(shù)

import pandas as pd

from keras.utils import to_categorical

導(dǎo)入數(shù)據(jù)

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

data.head()

導(dǎo)入機器學(xué)習(xí)庫

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)

把每個類別的數(shù)據(jù)提取出來

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()

展示

上述的代碼是在jupyter中運行的,所以是分開的

以上這篇pytorch 多分類問題,計算百分比操作就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • PyQt5界面無響應(yīng)的解決方案

    PyQt5界面無響應(yīng)的解決方案

    如果在主線程執(zhí)行耗時操作,比如 循環(huán)、sleep、wait 異步線程執(zhí)行 會導(dǎo)致 UI 界面進入無響應(yīng)狀態(tài),我們可以采用以下兩種方式異步處理:使用QThread 或 QTimer,本文給大家介紹了PyQt5界面無響應(yīng)的解決方案,需要的朋友可以參考下
    2024-05-05
  • 在Python中利用Pandas庫處理大數(shù)據(jù)的簡單介紹

    在Python中利用Pandas庫處理大數(shù)據(jù)的簡單介紹

    這篇文章簡單介紹了在Python中利用Pandas處理大數(shù)據(jù)的過程,Pandas庫的使用能夠很好地展現(xiàn)數(shù)據(jù)結(jié)構(gòu),是近來Python項目中經(jīng)常被使用使用的熱門技術(shù),需要的朋友可以參考下
    2015-04-04
  • Python 函數(shù)那不為人知的一面

    Python 函數(shù)那不為人知的一面

    通常我們定義一個函數(shù),然后調(diào)用該函數(shù)時,函數(shù)相關(guān)的代碼才開始執(zhí)行??墒呛芏嗳瞬⒉恢溃?dāng)我們定義函數(shù)時,一些代碼就開始執(zhí)行了。今天就來說說函數(shù)這個不為人知的一面
    2021-11-11
  • Python中八種數(shù)據(jù)導(dǎo)入方法總結(jié)

    Python中八種數(shù)據(jù)導(dǎo)入方法總結(jié)

    數(shù)據(jù)分析過程中,需要對獲取到的數(shù)據(jù)進行分析,往往第一步就是導(dǎo)入數(shù)據(jù)。導(dǎo)入數(shù)據(jù)有很多方式,不同的數(shù)據(jù)文件需要用到不同的導(dǎo)入方式,相同的文件也會有幾種不同的導(dǎo)入方式。下面總結(jié)幾種常用的文件導(dǎo)入方法
    2022-11-11
  • Pandas?多進程處理數(shù)據(jù)提高速度

    Pandas?多進程處理數(shù)據(jù)提高速度

    這篇文章主要介紹了Pandas?多進程處理數(shù)據(jù)提高速度,Pandas多進程的方法,pandarallel?庫,下面具體的測試方法,需要的朋友可以參考一下,希望對你的學(xué)習(xí)有所幫助
    2022-04-04
  • python3 中的字符串(單引號、雙引號、三引號)以及字符串與數(shù)字的運算

    python3 中的字符串(單引號、雙引號、三引號)以及字符串與數(shù)字的運算

    這篇文章主要介紹了python3 中的字符串(單引號、雙引號、三引號)以及字符串與數(shù)字的運算,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • 利用python中的matplotlib打印混淆矩陣實例

    利用python中的matplotlib打印混淆矩陣實例

    這篇文章主要介紹了利用python中的matplotlib打印混淆矩陣實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-06-06
  • Python按要求從多個txt文本中提取指定數(shù)據(jù)的代碼示例

    Python按要求從多個txt文本中提取指定數(shù)據(jù)的代碼示例

    本文給大家介紹了Python如何按要求從多個txt文本中提取指定數(shù)據(jù),遍歷文件夾并從中找到文件名稱符合我們需求的多個.txt格式文本文件,文中有相關(guān)的代碼示例供大家參考,具有一定的參考價值,需要的朋友可以參考下
    2023-12-12
  • 使用Scrapy爬取動態(tài)數(shù)據(jù)

    使用Scrapy爬取動態(tài)數(shù)據(jù)

    今天小編就為大家分享一篇關(guān)于使用Scrapy爬取動態(tài)數(shù)據(jù)的文章,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2018-10-10
  • python調(diào)用攝像頭的示例代碼

    python調(diào)用攝像頭的示例代碼

    這篇文章主要介紹了python調(diào)用攝像頭的示例代碼,幫助大家更好的理解和使用python,感興趣的朋友可以了解下
    2020-09-09

最新評論