2012-05-13 18 views
9

私はsciki-learnを使っているので、私を負担してください。意思決定ツリーを視覚化する(scikit-learnの例)

私は例を通過した http://scikit-learn.org/stable/modules/tree.html#tree

>>> from sklearn.datasets import load_iris 
>>> from sklearn import tree 
>>> iris = load_iris() 
>>> clf = tree.DecisionTreeClassifier() 
>>> clf = clf.fit(iris.data, iris.target) 
>>> from StringIO import StringIO 
>>> out = StringIO() 
>>> out = tree.export_graphviz(clf, out_file=out) 

どうやらgraphizファイルを使用する準備ができています。

しかし、graphizファイルを使用してツリーを描画するにはどうすればよいですか? (この例では、ツリーがどのように描かれているかについて詳しくは触れていませんでした)。

例コードとヒントは歓迎されている以上のものです。

ありがとうございます!


更新

私はあなたがどのOSを実行しない、Pythonの2.7.3

答えて

6

をUbuntuの12.04を使用していますか? graphvizがインストールされていますか?あなたの例では

StringIO()オブジェクト、データを確認する1つの方法があり、ここで、graphvizのデータを保持している:

... 
>>> print out.getvalue() 

digraph Tree { 
0 [label="X[2] <= 2.4500\nerror = 0.666667\nsamples = 150\nvalue = [ 50. 50. 50.]", shape="box"] ; 
1 [label="error = 0.0000\nsamples = 50\nvalue = [ 50. 0. 0.]", shape="box"] ; 
0 -> 1 ; 
2 [label="X[3] <= 1.7500\nerror = 0.5\nsamples = 100\nvalue = [ 0. 50. 50.]", shape="box"] ; 
0 -> 2 ; 
3 [label="X[2] <= 4.9500\nerror = 0.168038\nsamples = 54\nvalue = [ 0. 49. 5.]", shape="box"] ; 
2 -> 3 ; 
4 [label="X[3] <= 1.6500\nerror = 0.0407986\nsamples = 48\nvalue = [ 0. 47. 1.]", shape="box"] ; 
3 -> 4 ; 
5 [label="error = 0.0000\nsamples = 47\nvalue = [ 0. 47. 0.]", shape="box"] ; 
4 -> 5 ; 
6 [label="error = 0.0000\nsamples = 1\nvalue = [ 0. 0. 1.]", shape="box"] ; 
4 -> 6 ; 
7 [label="X[3] <= 1.5500\nerror = 0.444444\nsamples = 6\nvalue = [ 0. 2. 4.]", shape="box"] ; 
3 -> 7 ; 
8 [label="error = 0.0000\nsamples = 3\nvalue = [ 0. 0. 3.]", shape="box"] ; 
7 -> 8 ; 
9 [label="X[0] <= 6.9500\nerror = 0.444444\nsamples = 3\nvalue = [ 0. 2. 1.]", shape="box"] ; 
7 -> 9 ; 
10 [label="error = 0.0000\nsamples = 2\nvalue = [ 0. 2. 0.]", shape="box"] ; 
9 -> 10 ; 
11 [label="error = 0.0000\nsamples = 1\nvalue = [ 0. 0. 1.]", shape="box"] ; 
9 -> 11 ; 
12 [label="X[2] <= 4.8500\nerror = 0.0425331\nsamples = 46\nvalue = [ 0. 1. 45.]", shape="box"] ; 
2 -> 12 ; 
13 [label="X[0] <= 5.9500\nerror = 0.444444\nsamples = 3\nvalue = [ 0. 1. 2.]", shape="box"] ; 
12 -> 13 ; 
14 [label="error = 0.0000\nsamples = 1\nvalue = [ 0. 1. 0.]", shape="box"] ; 
13 -> 14 ; 
15 [label="error = 0.0000\nsamples = 2\nvalue = [ 0. 0. 2.]", shape="box"] ; 
13 -> 15 ; 
16 [label="error = 0.0000\nsamples = 43\nvalue = [ 0. 0. 43.]", shape="box"] ; 
12 -> 16 ; 
} 

あなたは.dot fileとしてそれを書いて、あなたがリンクされたソースに示したように、画像出力を生成することができます

$ dot -Tpng tree.dot -o tree.png(PNG形式の出力)

+0

こんにちは!私はUbuntu 12.04、Pythonバージョン2.7.3を使用しています。とにかく私がpythonスクリプトの中でそれをコマンドラインではできないのかどうか疑問に思っていましたか? – DjangoRocks

+1

もちろん、利用可能な[graphvizへのPythonバインディング](https://www.google.com/search?q=python+graphviz+binding)を手に入れてください。あなたはPythonシェル – theta

+0

の中からそれを実行できるはずです!それは助けになった! – DjangoRocks

4

非常に近いです!どうぞ:

graph_from_dot_data(out.getvalue()).write_pdf("somefile.pdf") 
+1

これは#classesが小さく、テキストのnvalue配列が行間で壊れていない場合にのみ有効です。この場合、\ nを手動で検索/置換しなければなりません(正当なものを保持します。コース)...痛みのビット。 1ホットコード化されたラベルのための同意...彼らはすぐにエラーをスローします。 – user1269942

関連する問題