獲取python運(yùn)行輸出的數(shù)據(jù)并解析存為dataFrame實(shí)例
在學(xué)習(xí)xg的 時候,想畫學(xué)習(xí)曲線,但無奈沒有沒有這個 evals_result_
AttributeError: 'Booster' object has no attribute 'evals_result_'
因?yàn)椴皇怯玫姆诸惼骰蛘呋貧w器,而且是使用的train而不是fit進(jìn)行訓(xùn)練的,看過源碼fit才有evals_result_這個,導(dǎo)致訓(xùn)練后沒有這個,但是又想獲取學(xué)習(xí)曲線,因此肯定還需要獲取訓(xùn)練數(shù)據(jù)。
運(yùn)行的結(jié)果 上面有數(shù)據(jù),于是就想自己解析屏幕的數(shù)據(jù)試一下,屏幕可以看到有我們迭代過程的數(shù)據(jù),因此想直接獲取屏幕上的數(shù)據(jù),思維比較low但是簡單粗暴。

接下來分兩步完成:
1) 獲取屏幕數(shù)據(jù)
import subprocess
import pandas as pd
top_info = subprocess.Popen(["python", "main.py"], stdout=subprocess.PIPE)
out, err = top_info.communicate()
out_info = out.decode('unicode-escape')
lines=out_info.split('\n')
注:這里的main.py就是自己之前執(zhí)行的python文件
2) 解析文件數(shù)據(jù):
ln=0
lst=dict()
for line in lines:
if line.strip().startswith('[{}] train-auc:'.format(ln)):
if ln not in lst.keys():
lst.setdefault(ln, {})
tmp = line.split('\t')
t1=tmp[1].split(':')
t2=tmp[2].split(':')
if str(t1[0]) not in lst[ln].keys():
lst[ln].setdefault(str(t1[0]), 0)
if str(t2[0]) not in lst[ln].keys():
lst[ln].setdefault(str(t2[0]), 0)
lst[ln][str(t1[0])]=t1[1]
lst[ln][str(t2[0])]=t2[1]
ln+=1
json_df=pd.DataFrame(pd.DataFrame(lst).values.T, index=pd.DataFrame(lst).columns, columns=pd.DataFrame(lst).index).reset_index()
json_df.columns=['numIter','eval-auc','train-auc']
print(json_df)
整體代碼:
import subprocess
import pandas as pd
top_info = subprocess.Popen(["python", "main.py"], stdout=subprocess.PIPE)
out, err = top_info.communicate()
out_info = out.decode('unicode-escape')
lines=out_info.split('\n')
ln=0
lst=dict()
for line in lines:
if line.strip().startswith('[{}] train-auc:'.format(ln)):
if ln not in lst.keys():
lst.setdefault(ln, {})
tmp = line.split('\t')
t1=tmp[1].split(':')
t2=tmp[2].split(':')
if str(t1[0]) not in lst[ln].keys():
lst[ln].setdefault(str(t1[0]), 0)
if str(t2[0]) not in lst[ln].keys():
lst[ln].setdefault(str(t2[0]), 0)
lst[ln][str(t1[0])]=t1[1]
lst[ln][str(t2[0])]=t2[1]
ln+=1
json_df=pd.DataFrame(pd.DataFrame(lst).values.T, index=pd.DataFrame(lst).columns, columns=pd.DataFrame(lst).index).reset_index()
json_df.columns=['numIter','eval-auc','train-auc']
print(json_df)
看下效果:

以上這篇獲取python運(yùn)行輸出的數(shù)據(jù)并解析存為dataFrame實(shí)例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
OpenCV 使用imread()函數(shù)讀取圖片的六種正確姿勢
這篇文章主要介紹了OpenCV 使用imread()函數(shù)讀取圖片的六種正確姿勢,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
Python統(tǒng)計列表中每個元素出現(xiàn)次數(shù)的4種實(shí)現(xiàn)
本文主要介紹了Python統(tǒng)計列表中每個元素出現(xiàn)次數(shù)的4種實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07
python 使用re.search()篩選后 選取部分結(jié)果的方法
今天小編就為大家分享一篇python 使用re.search()篩選后 選取部分結(jié)果的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-11-11

