4

I'm trying to display a decision tree in Jupyter Notebook and I keep receiving the message:

CalledProcessError: Command '['dot.bat', '-Tsvg']' returned non-zero exit status 1

I'm using the following code:

from sklearn.datasets import load_iris 
from sklearn import tree
import graphviz
from IPython.display import SVG
iris = load_iris()
clf = tree.DecisionTreeClassifier()
fitted_clf = clf.fit(iris.data, iris.target)
graph = graphviz.Source(tree.export_graphviz(fitted_clf, 
                               feature_names = iris.feature_names,
                               class_names = iris.target_names, 
                               filled = True, rounded = True, 
                               special_characters = True))
SVG(graph.pipe(format='svg'))

The Exception is raised in the last line when I try to use 'pipe'. I also tried:

graph.format = 'png'
graph.render('example')

instead of pipe but i keep on raising a similar exception:

CalledProcessError: Command '['dot.bat', '-Tpng', '-O', 'example']' returned non-zero exit status 1

Any idea of what is causing that behaviour? and how can I fix it?

(I'm using Python 3.5.2, sklearn 0.17.1, graphviz 0.8.2 and IPython 6.4.0)

Chris Tang
  • 561
  • 7
  • 16
Rafa.Rugamas
  • 65
  • 2
  • 8

2 Answers2

3

Installing graphviz xorg-libxrender xorg-libxpm from conda-forge repo, and the python bindings from pip usually solves this for me.

conda install -c conda-forge graphviz xorg-libxrender xorg-libxpm
pip install graphviz

Do not forget to uninstall the previously installed packages first.

  • 3
    This did not work for me: the second part is not correctly bound to the first part. The solution that worked for me is to do `conda install python-graphviz` instead of `pip install graphviz`: see also https://stackoverflow.com/a/47043173/7262247 – smarie Jun 08 '20 at 16:06
0

Paul-Armand's answer should work if you are working with conda. If not then you have to run :

brew install graphviz
pip install graphviz

In case you get a warning saying that graphviz is already installed but not linked then follow the instruction to link it. I.e brew link graphviz (or brew link --overwrite graphviz if the former gives an error).

The reason it works in conda without brew is that conda install graphviz actually installs the c++ library not the python one.

Yann Dubois
  • 1,029
  • 12
  • 14