2016-08-25 3 views
0

Ich kann ein einfaches Beispiel nicht reproduzieren. Hier ist, wie es geht:Wie man einen Baum mit graphviz grafisch darstellt?

import pandas as pd 
import numpy as np 
import sklearn as skl 
from sklearn import tree 
from sklearn.cross_validation import train_test_split as tts 



# import data and give a little overview 
sample = pd.read_stata('sample_data.dta') 
s = sample 

# Let's learn 
X = np.array((s.crisis, s.cash, s.industry, s.current_debt, s.activity)).reshape(1000, 5) 
y = np.array(s.wc_measure) 

X_train, X_test, y_train, y_test = tts(X, y, test_size = .8) 

my_tree = tree.DecisionTreeClassifier() 
clf = my_tree.fit(X_train, y_train) 
predictions = my_tree.predict(X_test) 

# Graph the tree 
from sklearn.externals.six import StringIO 
import pydotplus 
dotfile = StringIO() 
tree.export_graphviz(clf, out_file=dotfile) 
pydotplus.graph_from_dot_data(dotfile.getvalue()).write_png("my_tree.png") 

Wenn ich versuche, den Baum grafisch darzustellen, bekomme ich folgende Fehlermeldung:

pydotplus.graphviz.InvocationException: GraphViz's executables not found 

Jede Idee, was das Problem ist? Ich folge graphvids Dokumentation.

+0

Ihr Problem ist nur mit 'Baum' oder mit einem Graph Graph Graph? –

+0

Es läuft bis tree.export:/ – Rachel

Antwort

1

Es scheint, dass Sie die graphviz-Software überhaupt nicht installiert haben. Sie sollten download und installieren Sie es vor der Verwendung des Moduls.

+0

Ich habe 'pip installiert graphviz' ... so sollte das tun, sollte es nicht? – Rachel

+0

Nein, es ist nicht genug. 'pip install graphviz' installiert nur das Python-Modul, das die graphviz-Software verwendet. Sehen Sie [hier] (https://pypi.python.org/pypi/graphviz) im Installationsabschnitt. –