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

所属分类: 脚本专栏 / python 阅读数: 764
收藏 0 赞 0 分享

在学习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)

看下效果:

以上这篇获取python运行输出的数据并解析存为dataFrame实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

pandas的qcut()方法详解

这篇文章主要介绍了pandas的qcut()方法详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

从列表或字典创建Pandas的DataFrame对象的方法

这篇文章主要介绍了从列表或字典创建Pandas的DataFrame对象的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

pandas.DataFrame的pivot()和unstack()实现行转列

这篇文章主要介绍了pandas.DataFrame的pivot()和unstack()实现行转列,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

pandas中的series数据类型详解

这篇文章主要介绍了pandas中的series数据类型详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

pandas 时间格式转换的实现

这篇文章主要介绍了pandas 时间格式转换的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

python中时间、日期、时间戳的转换的实现方法

这篇文章主要介绍了python中时间、日期、时间戳的转换的实现方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

pandas进行时间数据的转换和计算时间差并提取年月日

这篇文章主要介绍了pandas进行时间数据的转换和计算时间差并提取年月日,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

详解将Pandas中的DataFrame类型转换成Numpy中array类型的三种方法

这篇文章主要介绍了详解将Pandas中的DataFrame类型转换成Numpy中array类型的三种方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

python和c语言的主要区别总结

在本篇文章里小编给各位整理了关于python和c语言的主要区别的相关知识帖内容,有需要的朋友们学习阅读下。
收藏 0 赞 0 分享

选择Python写网络爬虫的优势和理由

在本篇文章里小编给各位整理了一篇关于选择Python写网络爬虫的优势和理由以及相关代码实例,有兴趣的朋友们阅读下吧。
收藏 0 赞 0 分享
查看更多