2016-10-01 3 views
1

sklearnを使用して意思決定ツリーをエクスポートしたいと思います。私は意思決定ツリーをエクスポートするために、以下の方法を使用しその後SckitはGraphVizで学習して空の出力をエクスポートします

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

まず私は、決定木分類器を訓練し正常に終了しました。 ファイルは作成されていますが、空です。 私は何が間違っていますか?ここで

+0

、誰もが自分のコードを実行し、エラーを見ることができるようにあなたには、いくつかのデータが含まれている場合はhelpo迅速に得る可能性があります。 – tfv

答えて

0

は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") 
+0

あなたの方法を使用すると、エラーが発生します:graph.write_pdf( "dtree.pdf") {graph '|AttributeError: 'NoneType'オブジェクトには属性 'write_pdf'がありません –

+0

どのようにそれを避けるのですか? –

関連する問題