在DecisionTree中使用StringIO的目的是什么 [英] What is the purpose of using StringIO in DecisionTree

查看:105
本文介绍了在DecisionTree中使用StringIO的目的是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写决策树,以下代码是完整代码的一部分:

I am writing a decision tree and the following code is a part of the complete code:

def show_tree(tree, features, path):
    f = io.StringIO()
    export_graphviz(tree, out_file=f, feature_names=features)
    pydotplus.graph_from_dot_data(f.getvalue()).write_png(path)
    img = misc.imread(path)
    plt.rcParams['figure.figsize'] = (20,20)
    plt.imshow(img)

有人可以告诉我在这里使用StringIO的目的是什么吗?

Could any one please tell me what is the purpose of using StringIO here?

推荐答案

Python不是我的主要语言,但是我认为您的问题的答案非常简单,不需要大量研究.

Python is not my leading language, however I think answer for your question is quite simple and does not require lot of research.

StringIO在这里用于维护输入/输出文本流.您的函数显示树,但是为此,它需要一种实现它的方法,即某种数据传输高速公路.

StringIO is used here to maintain Input/Output text stream. Your function show tree, however for doing that it needs a way to do it, some kind of data transport highway.

在这里f = io.StringIO()您正在初始化数据流.之后,在这种特殊情况下,您可以随意使用它:

Here f = io.StringIO() you're initializing your data stream. After that you are free to use it as you want, in this particular case:

  1. export_graphviz(tree, out_file=f, feature_names=features)

此处:out_file=f使用f = io.StringIO();之前的初始化数据将数据导出到流中.由于StringIO是内存中的文本文件,因此您基本上将数据放在流对象中以备将来使用.因此,您不必将数据写入 .dot 文件中,而只需将其临时保存(并且临时意味着只要使用流)

Here: out_file=f you export data to your stream using initialized before f = io.StringIO();. As StringIO is in-memory text file, you basically put your data aside in stream object for further use. Thanks to that you don't have to write your data into .dot file, instead you temporary hold it.(And temporary means for as long as your stream is in use)

有关此特定案例的更多信息

  1. pydotplus.graph_from_dot_data(f.getvalue()).write_png(path)

在这里:f.getvalue()您是从 .dot 数据生成图形的.在最基本的用法中,应确保存储以前生成的数据的 .dot 文件的路径,但不必这样做!这就是诀窍,您的数据仍在您事先创建并填充的流对象中!因此,现在您要做的就是直接将其定向到此函数,该函数将使用该数据生成图形图像并将其另存为 .png 文件.

Here: f.getvalue() you generate your graph from .dot data. In the most basic use you should ensure path to .dot file in which previously generated data would be stored, but YOU DON'T HAVE TO! That is the trick, your data is still in stream object which you created and filled beforehand! So now all you have to do is direct it straight to this function which will generate your graph image with that data and save it as .png file.

可以通过多种方式建立系统文件与程序之间的通信,但通常使用流.您在一开始就初始化流,使用它然后关闭.每个std::coutstd:err(我的主要语言参考,对于该非python示例都很抱歉)就是该流. Stream允许您维护程序和指定的标记(例如控制台,在这种情况下为文件)之间的数据交换,但是您也可以将其用作临时存储空间,在这种情况下,由于不使用它,可以加快图像生成过程确实必须将数据写入文件并加载到文件中.要做的全部工作就是按照其他函数可以接受的顺序将数据写入流,然后使用完全相同的流读取该数据以生成图像.

Communication between system files and your program can be established in many ways but usually you use streams. You initialize stream at the very beginning, use it and then close. Every std::cout or std:err(my main language reference, sorry for that non-python example) is that stream. Stream allows you to maintain data exchange between your program and designated tagret(e.g. console, or in this case file), however you can use it also as temporary storage space which in that particular case will speed up image generation process as you don't really have to write and load data into file. All you have to do thanks to that is writing data to stream in an order which other function will accept, and then use the very same stream to read that data for image generation purposes.

有关StringIO的更多信息

这篇关于在DecisionTree中使用StringIO的目的是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆