近來(lái)實(shí)驗(yàn)室的師姐要發(fā)論文,由于論文交稿時(shí)間臨近,有一些雜活兒需要處理,作為實(shí)驗(yàn)室資歷最淺的一批,我這個(gè)實(shí)習(xí)生也就責(zé)無(wú)旁貸地幫忙當(dāng)個(gè)下手。今天師姐派了一個(gè)小活,具體要求是:
給一些訓(xùn)練模型的迭代次數(shù),訓(xùn)練精度的數(shù)據(jù),讓我做成圖表形式展示出來(lái),一方面幫助檢查模型訓(xùn)練時(shí)的不足,另一方面來(lái)看樣本數(shù)目和預(yù)測(cè)精度之間的聯(lián)系,數(shù)據(jù)具體格式如下:
Iteration 1500 label train test right acc 12 143 24 24 1.0 160 92 16 15 0.9375 100 12 2 0 0.0 142 0 0 0 0.0 152 0 0 0 0.0 110 10 2 0 0.0 170 12 2 2 1.0 42 421 70 63 0.9 31 43 8 5 0.625 22 132 22 18 0.818181818182 60 51 9 8 0.888888888889 51 916 153 143 0.934640522876 131 82 14 11 0.785714285714 53 84 14 10 0.714285714286 70 9 2 2 1.0 21 531 89 89 1.0 120 1 1 1 1.0 11 454 76 71 0.934210526316 90 1 1 1 1.0 32 39 7 6 0.857142857143 41 151 25 14 0.56 132 0 0 0 0.0 151 43 7 6 0.857142857143 43 8 2 1 0.5 80 7 2 1 0.5 141 96 16 16 1.0 44 67 12 2 0.166666666667 right: 509 accuracy:0.883680555556
我的任務(wù)就是以label為自變量,繪制出它和train及acc之間的關(guān)系。
接到這個(gè)任務(wù)后,最直觀(guān)的感受就是常規(guī)的洗數(shù)據(jù),于是我先把這些數(shù)據(jù)放在txt文件中存儲(chǔ)下來(lái),由于每個(gè)數(shù)據(jù)之間的間隔大于一個(gè)空格,我想當(dāng)然地寫(xiě)個(gè)正則匹配腳本將數(shù)據(jù)間的大空格轉(zhuǎn)換為一個(gè)逗號(hào)(轉(zhuǎn)換為逗號(hào)的目的是這樣可以直接轉(zhuǎn)換為CSV表格文件,然而在本次任務(wù)中貌似意義不大….)
#**********************Python 3.6.1***************************# #* 將txt文本數(shù)據(jù)中的過(guò)長(zhǎng)的空格更為一個(gè)逗號(hào) *# #***************** Author LQ ******************************# #********************** 2018/4/4 ****************************# #!/usr/bin/python # -*- coding: utf-8 -*- import re import os #os模塊與文本操作直接相關(guān)的模塊 #*********下面三句代碼作用不詳,就是為了防止出現(xiàn)編碼問(wèn)題********* import importlib import sys importlib.reload(sys) #**************************************************** PATTERN = '\s+'#匹配出文本中的長(zhǎng)空格 class Cleaner: #初始化 def __init__(self): os.chdir('D:\\Learning\\Machine_Learning\\實(shí)習(xí)\\師姐論文實(shí)驗(yàn)') #改變工作目錄到txt文件對(duì)應(yīng)的目錄 self.content = open("acc-onlyRealImage-Iter2500.txt") def grab_content(self): line=self.content.readline() pre=re.compile(PATTERN) while line: line_1=pre.sub(',',line) #將文本的長(zhǎng)空格轉(zhuǎn)換為逗號(hào)后,利于轉(zhuǎn)成CSV格式,然后label按照升序排列 self.Write_content(line_1) line = self.content.readline() def Write_content(self,line_1): path='acc-onlyRealImage-Iter2500-after.txt' f=open(path,'a') f.write('\n'+line_1) def run(self): self.grab_content() if __name__ == '__main__': cleaner = Cleaner() cleaner.run()
數(shù)據(jù)清洗完成后,自然就是繪圖了,逛了一些博客后,著手寫(xiě)個(gè)腳本,第一版是繪制出label和train及acc的雙Y軸折線(xiàn)圖,腳本較為簡(jiǎn)單,就是調(diào)用別人造的輪子,直接附上代碼:
#**********************Python 3.6.1***************************# #* 繪制出雙Y軸折線(xiàn)圖 *# #***************** Author LQ ******************************# #********************** 2018/4/4 ****************************# #!/usr/bin/python # -*- coding: utf-8 -*- import re import os #os模塊與文本操作直接相關(guān)的模塊 import matplotlib.pyplot as plt import numpy as np #*********下面三句代碼作用不詳,就是為了防止出現(xiàn)編碼問(wèn)題********* import importlib import sys importlib.reload(sys) #**************************************************** font2 = {'family' : 'Times New Roman', 'weight' : 'normal', 'size' : 18, } class Drawing: #初始化 def __init__(self): os.chdir('D:\\Learning\\Machine_Learning\\實(shí)習(xí)\\師姐論文實(shí)驗(yàn)') #改變工作目錄到指定文件目錄 self.content = open("acc-onlyRealImage-Iter2200-after.txt") self.content1 = open("acc-onlyRealImage-Iter2500-after.txt") def grab_content(self): lines=self.content.readlines() lines_1=self.content1.readlines() x_1 = [line.strip().split(',')[0] for line in lines ]#字段以逗號(hào)分隔,這里取得是第4列 y_train_1=[line.strip().split(',')[1] for line in lines ] y_train_2=[line.strip().split(',')[1] for line in lines_1 ] y_acc_1=[line.strip().split(',')[4] for line in lines ] y_acc_2=[line.strip().split(',')[4] for line in lines_1 ] x = list(range(len(x_1))) y_acc=[] y_acc1=[] y_train=[] y_train1=[] for i in range(len(y_acc_1)): y_acc.append(float(y_acc_1[i])) y_acc1.append(float(y_acc_2[i])) y_train.append(int(y_train_1[i])) y_train1.append(int(y_train_2[i])) #plt.xticks(x, x_1,rotation=0) fig,left_axis=plt.subplots() p1, =left_axis.plot(x, y_train,'ro-') right_axis = left_axis.twinx() p2, =right_axis.plot(x, y_acc,'bo-') plt.xticks(x, x_1,rotation=0) #設(shè)置x軸的顯示形式 #設(shè)置左坐標(biāo)軸以及右坐標(biāo)軸的范圍、精度 left_axis.set_ylim(0,1201) left_axis.set_yticks(np.arange(0,1201,200)) right_axis.set_ylim(0,1.01) right_axis.set_yticks(np.arange(0,1.01,0.20)) #設(shè)置坐標(biāo)及標(biāo)題的大小、顏色 left_axis.set_title('RealAndSimulation-Iter6600',font2) left_axis.set_xlabel('Labels',font2) left_axis.set_ylabel('Number of training sets',font2,color='r') left_axis.tick_params(axis='y', colors='r') right_axis.set_ylabel('Accuracy',font2,color='b') right_axis.tick_params(axis='y', colors='b') plt.show() def run(self): self.grab_content() if __name__ == '__main__': Drawing = Drawing() Drawing.run()
繪制出的圖形如上所示,其實(shí)看起來(lái)也還不錯(cuò),不過(guò)師姐表示有點(diǎn)亂,建議做個(gè)柱形的看看,于是繼續(xù)擼代碼:
#**********************Python 3.6.1***************************# #* 繪制單Y軸雙變量柱狀圖 *# #***************** Author LQ ******************************# #********************** 2018/4/4 ****************************# #!/usr/bin/python # -*- coding: utf-8 -*- import re import os #os模塊與文本操作直接相關(guān)的模塊 import matplotlib.pyplot as plt import numpy as np #*********下面三句代碼作用不詳,就是為了防止出現(xiàn)編碼問(wèn)題********* import importlib import sys importlib.reload(sys) #**************************************************** font2 = {'family' : 'Times New Roman', #設(shè)置字體 'weight' : 'normal', 'size' : 18, } class Drawing: #初始化 def __init__(self): os.chdir('D:\\Learning\\Machine_Learning\\實(shí)習(xí)\\師姐論文實(shí)驗(yàn)') #改變工作目錄到指定文件的目錄 self.content = open("acc-onlyRealImage-Iter2200-after.txt") self.content1 = open("acc-onlyRealImage-Iter2500-after.txt") def autolabel(self,rects,y): #在柱狀圖上面添加 數(shù)值 i=0 for rect in rects: #讀出列表存儲(chǔ)的value值 value=y[i] x_1 = rect.get_x() + rect.get_width()/2 y_1 = rect.get_height() #x_1,y_1對(duì)應(yīng)柱形的橫、縱坐標(biāo) i+=1 plt.text(x_1, y_1, value, ha='center', va='bottom',fontdict={'size': 8}) #在fontdict中設(shè)置字體大小 rect.set_edgecolor('white') def Pictures(self): lines=self.content.readlines() lines_1=self.content1.readlines() x_1 = [line.strip().split(',')[0] for line in lines ]#字段以逗號(hào)分隔,這里取得是第1列 y_train_1=[line.strip().split(',')[1] for line in lines ] y_train_2=[line.strip().split(',')[1] for line in lines_1 ] y_acc_1=[line.strip().split(',')[4] for line in lines ] y_acc_2=[line.strip().split(',')[4] for line in lines_1 ] x = list(range(len(x_1))) y_acc=[] y_acc1=[] y_train=[] y_train1=[] for i in range(len(y_acc_1)): y_acc.append(float(y_acc_1[i])) y_acc1.append(float(y_acc_2[i])) y_train.append(int(y_train_1[i])) y_train1.append(int(y_train_2[i])) plt.xticks(x, x_1,rotation=0) #設(shè)置X軸坐標(biāo)值為label值 for i in range(len(x)): #調(diào)整柱狀圖的橫坐標(biāo),使得打印出來(lái)的圖形看起來(lái)更加舒服 x[i] = x[i] -0.2 a=plt.bar(x, y_train,width=0.4,label='iter2200',fc = 'b') #a=plt.bar(x, y_acc,width=0.4,label='iter2200',fc = 'b') for i in range(len(x)): x[i] = x[i] + 0.4 b=plt.bar(x, y_train1, width=0.4, label='iter2500',fc = 'r') #b=plt.bar(x, y_acc1, width=0.4, label='iter2500',fc = 'r') plt.xlabel('Labels',font2) #設(shè)置Y軸值的范圍 plt.ylim((0, 1000)) #設(shè)置Y軸的刻度值 plt.yticks(np.arange(0,1001, 200)) #plt.ylim((0, 1.1)) #plt.yticks(np.arange(0,1.1, 0.2)) #plt.ylabel('Accuracy',font2) plt.ylabel('Number of training sets',font2) #字體的格式在font2中有設(shè)置 self.autolabel(a,y_train_1) #為柱形圖打上數(shù)值標(biāo)簽 self.autolabel(b,y_train_2) #self.autolabel(a,y_acc_1) #self.autolabel(b,y_acc_2) #plt.title("RealAndSimulation",font2) plt.title("OnlyRealImage",font2) plt.legend() plt.show() def run(self): self.Pictures() if __name__ == '__main__': Draw = Drawing() Draw.run()
呈現(xiàn)的效果如下,此處因?yàn)閷?duì)于雙柱形圖通常采用同一Y軸坐標(biāo)系,所以此處選擇的是比對(duì)不同迭代次數(shù):
此處為了方便實(shí)驗(yàn)結(jié)果的觀(guān)測(cè),在每個(gè)柱形上面均打印出了對(duì)應(yīng)的數(shù)值,至此,這部分的任務(wù)ending,難度不是很大,不過(guò)需要自己耐心編寫(xiě)腳本,調(diào)試出好的結(jié)果~
以上這篇python繪制雙Y軸折線(xiàn)圖以及單Y軸雙變量柱狀圖的實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
