5

I'm unable to visualize or write the Decision tree. How can I go about it?

Python version 3.5, Anaconda 3, I have even set the environment variables

    from sklearn import tree
    model = tree.DecisionTreeClassifier(criterion='gini') 
    model=tree.DecisionTreeClassifier()
    model.fit(trainData,trainLabel)
    model.score(trainData,trainLabel)
    predicted= model.predict(testData)

    from sklearn.externals.six import StringIO
    import pydot 
    import pydotplus
    dot_data = StringIO() 
    tree.export_graphviz(model, out_file=dot_data) 
    graph = pydotplus.graph_from_dot_data(dot_data.getvalue()) 
    print(graph)
    graph.write_pdf("C:\\Users\\anagha\\Desktop\\SynehackData\\DATA\\DATA\\graph.pdf") 

error :

InvocationException: GraphViz's executables not found
halfer
  • 19,824
  • 17
  • 99
  • 186
Anagha
  • 3,073
  • 8
  • 25
  • 43
  • Please read [Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers?](https://meta.stackoverflow.com/q/326569) - the summary is that this is not an ideal way to address volunteers, and is probably counterproductive to obtaining answers. Please refrain from adding this to your questions. – halfer Apr 21 '17 at 07:41

4 Answers4

1

I understand the thread is a little old but today I got the same error when trying to visualize a Bayesian Network in a Jupyter notebook with the PyAgrum library.

I'm on Windows 10 using the Anaconda package management. In my case I needed to install the package python-graphviz using the following command:

conda install python-graphviz

After the installation, I just needed to restart the jupyter kernel and run the code again.

Helder
  • 482
  • 5
  • 18
0

I got this error and tried a million things. I saw that I should add to the environment variable, 'path' if you're in Windows. I did this, restarted, Python, but it didn't work. I did it for graphviz and for pydotplus.

Then I saw someone used a slightly different path from what I had used. something like Drive:\Users\User.Name\AppData\Local\Continuum\anaconda3\envs\MyVirtualEnv\Library\bin\graphviz So I added that to the path, and restarted all things anaconda. This was probably the 98th thing I tried. It worked!

I had been using a path like Drive:\Users\User.Name\AppData\Local\Continuum\anaconda3\envs\MyVirtualEnv\lib\site-packages\graphviz, which didn't work, but I put in both, and a similar one for pydotplus.

Vince Hall
  • 46
  • 1
  • 6
0

The other solutions didn't help for me.

I already had the following installed:

graphviz==2.50.0
pydotplus==2.0.2
pydot==1.4.1

But running whereis dot and whereis graphviz, it was clear that I was still missing the graphviz library on my operating system: for dot, the whereis command returned a path on my system, for graphviz, no path was printed by the whereis command.

What helped for me (on Ubuntu) is running sudo apt-get install graphviz, since the PyPi page of the python package graphviz (https://pypi.org/project/graphviz/) mentions the following:

To render the generated DOT source code, you also need to install Graphviz (download page, archived versions, installation procedure for Windows).

The download page linked to above mentioned that sudo apt-get install graphviz was the way to go on Ubuntu. If you have another operating system, check the Download page above for a way to install graphviz on your specific OS.

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Sander Vanden Hautte
  • 2,138
  • 3
  • 22
  • 36
-4

You can take help of this code !!

import pydotplus
from sklearn.datasets import load_iris
from sklearn import tree
import collections

# Data Collection
X = [ [180, 15,0],     
      [177, 42,0],
      [136, 35,1],
      [174, 65,0],
      [141, 28,1]]

Y = ['man', 'woman', 'woman', 'man', 'woman']    

data_feature_names = [ 'height', 'hair length', 'voice pitch' ]

# Training
clf = tree.DecisionTreeClassifier()
clf = clf.fit(X,Y)
# Visualize data
dot_data = tree.export_graphviz(clf,
                                feature_names=data_feature_names,
                                out_file=None,
                                filled=True,
                                rounded=True)
graph = pydotplus.graph_from_dot_data(dot_data)

colors = ('turquoise', 'orange')
edges = collections.defaultdict(list)

for edge in graph.get_edge_list():
    edges[edge.get_source()].append(int(edge.get_destination()))

for edge in edges:
    edges[edge].sort()    
    for i in range(2):
        dest = graph.get_node(str(edges[edge][i]))[0]
        dest.set_fillcolor(colors[i])

graph.write_png('tree.png')