【问题标题】:Sckit learn with GraphViz exports empty outputsScikit learn with GraphViz 导出空输出
【发布时间】:2017-02-10 03:17:22
【问题描述】:

我想使用 sklearn 导出决策树。

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

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

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

之后我使用以下方法导出决策树:

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")

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

【问题讨论】:

  • 如果您包含一些数据以便任何人都可以运行您的代码并查看错误,您可能会更快地获得帮助。

标签: scikit-learn graphviz decision-tree pydot


【解决方案1】:

这是一个对我有用的输出示例,使用 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")

【讨论】:

  • 如果我使用你的方式,我会得到一个错误:graph.write_pdf("dtree.pdf") Expected {'graph' | 'digraph'} (at char 0), (line:1, col:1) AttributeError: 'NoneType' object has no attribute 'write_pdf'
  • 知道如何避免吗?
猜你喜欢
  • 2018-08-10
  • 2023-03-22
  • 1970-01-01
  • 2015-08-26
  • 2013-11-01
  • 2018-06-27
  • 2020-11-22
相关资源
最近更新 更多