Sckit Learn with GraphViz导出空输出 [英] Sckit learn with GraphViz exports empty outputs

查看:157
本文介绍了Sckit Learn with GraphViz导出空输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用sklearn导出决策树.

I would like to export decision tree using sklearn.

首先,我训练了决策树分类器:

First I trained a decision tree classifier:

self._selected_classifier = tree.DecisionTreeClassifier()
self._selected_classifier.fit(train_dataframe, train_class)

self._column_names = list(train_dataframe.columns.values)

此后,我使用以下方法来导出决策树:

After that I used the following method in order to export the decision tree:

def _create_graph_visualization(self):
    decision_tree_classifier = self._selected_classifier 

    from sklearn.externals.six import StringIO
    dot_data = StringIO()
    tree.export_graphviz(decision_tree_classifier,
                         out_file=dot_data,
                         feature_names=self._column_names)
    import pydotplus
    graph = pydotplus.graph_from_dot_data(dot_data.getvalue())
    graph.write_pdf("decision_tree_output.pdf")

在遇到许多有关缺少可执行文件的错误之后,该程序现在已成功完成. 文件已创建,但为空. 我究竟做错了什么?

After many errors regarding missing executables now the program is finished successfully. The file is created, but it is empty. What am I doing wrong?

推荐答案

下面是一个使用pydotplus的输出示例,适用于我:

Here is an example with output which works for me, using pydotplus:

from sklearn import tree  
import pydotplus
import StringIO

# Define training and target set for the classifier
train = [[1,2,3],[2,5,1],[2,1,7]]
target = [10,20,30]

# Initialize Classifier. Random values are initialized with always the same random seed of value 0 
# (allows reproducible results)
dectree = tree.DecisionTreeClassifier(random_state=0)
dectree.fit(train, target)

# Test classifier with other, unknown feature vector
test = [2,2,3]
predicted = dectree.predict(test)

dotfile = StringIO.StringIO()
tree.export_graphviz(dectree, out_file=dotfile)
graph=pydotplus.graph_from_dot_data(dotfile.getvalue())
graph.write_png("dtree.png")
graph.write_pdf("dtree.pdf")

这篇关于Sckit Learn with GraphViz导出空输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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