How to graph a tree with graphviz?

前提是你 提交于 2019-12-11 05:28:34

问题


I can't reproduce a simple example. Here is how it goes:

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

When I try to graph the tree, I get the following error:

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

Any idea what the problem is? I neatly follow graphviz's documentation.


回答1:


It seem you didn't install the graphviz software at all. You should download and install it before using the module.




回答2:


step 1, install C-version of graphviz using 'sudo apt-get install graphviz' if ubuntu, 'brew install graphviz' if OSX

step 2, install package 'graphviz' by pip sudo pip install graphviz



来源:https://stackoverflow.com/questions/39146728/how-to-graph-a-tree-with-graphviz

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!