Sckit learn with GraphViz输出空输出



我想使用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")

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

下面是一个使用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")

相关内容

  • 没有找到相关文章