当前位置: 移动技术网 > IT编程>脚本编程>Python > 获取python运行输出的数据并解析存为dataFrame

获取python运行输出的数据并解析存为dataFrame

2020年07月03日  | 移动技术网IT编程  | 我要评论

在学习xg的 时候,想画学习曲线,但无奈没有没有这个 evals_result_

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

    因为不是用的分类器或者回归器,而且是使用的train而不是fit进行训练的,看过源码fit才有evals_result_这个,导致训练后没有这个,但是又想获取学习曲线,因此肯定还需要获取训练数据。

    运行的结果 上面有数据,于是就想自己解析屏幕的数据试一下,屏幕可以看到有我们迭代过程的数据,因此想直接获取屏幕上的数据,思维比较low但是简单粗暴。

接下来分两步完成: 

1)获取屏幕数据

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就是自己之前执行的python文件

2)解析文件数据:

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)

看下效果:

本文地址:https://blog.csdn.net/zhou_438/article/details/107058564

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网