国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術文章
文章詳情頁

獲取python運行輸出的數據并解析存為dataFrame實例

瀏覽:7日期:2022-07-18 11:42:18

在學習xg的 時候,想畫學習曲線,但無奈沒有沒有這個 evals_result_

AttributeError: ’Booster’ object has no attribute ’evals_result_’

因為不是用的分類器或者回歸器,而且是使用的train而不是fit進行訓練的,看過源碼fit才有evals_result_這個,導致訓練后沒有這個,但是又想獲取學習曲線,因此肯定還需要獲取訓練數據。

運行的結果 上面有數據,于是就想自己解析屏幕的數據試一下,屏幕可以看到有我們迭代過程的數據,因此想直接獲取屏幕上的數據,思維比較low但是簡單粗暴。

獲取python運行輸出的數據并解析存為dataFrame實例

接下來分兩步完成:

1) 獲取屏幕數據

import subprocessimport pandas as pdtop_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就是自己之前執行的python文件

2) 解析文件數據:

ln=0lst=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+=1json_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 subprocessimport pandas as pdtop_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=0lst=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+=1json_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運行輸出的數據并解析存為dataFrame實例

以上這篇獲取python運行輸出的數據并解析存為dataFrame實例就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持好吧啦網。

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 新巴尔虎左旗| 临汾市| 四平市| 上栗县| 田东县| 洛扎县| 静乐县| 土默特右旗| 石首市| 三门峡市| 桓台县| 祁东县| 南木林县| 古丈县| 基隆市| 永嘉县| 贵南县| 昔阳县| 客服| 固始县| 宣恩县| 武邑县| 乐都县| 霍山县| 侯马市| 永和县| 陕西省| 绥中县| 通渭县| 海安县| 澄城县| 金坛市| 嘉鱼县| 通州区| 虎林市| 临清市| 海阳市| 潼南县| 恩平市| 梧州市| 怀宁县|