0

I'm attempting to use GraphViz to visualize a decision tree with Google Cloud Datalab, but the following error is being thrown:

InvocationException: GraphViz's executables not found

I found a related post, but the solutions here did not resolve the problem in Datalab.

Run the following to replicate:

!pip install graphviz

import graphviz 
import numpy as np
from sklearn.datasets import load_iris
from sklearn import tree
from sklearn.externals.six import StringIO

iris = load_iris()
train_data = iris.data
train_labels = iris.target

clf = tree.DecisionTreeClassifier()
clf.fit(train_data, train_labels)

dot_data = tree.export_graphviz(clf, out_file=None, 
                         feature_names=iris.feature_names,  
                         class_names=iris.target_names)  
graph = graphviz.Source(dot_data)  
graph 

2 Answers2

2

For me, it worked on Datalab (Python2 kernel) after running these cells:

%%bash
apt-get update -y

(this will most likely give you an error about repositories not being signed but you can proceed anyway with the --allow-unauthenticated flag)

%%bash
apt-get install python-pydot -y --allow-unauthenticated

enter image description here

Guillem Xercavins
  • 6,938
  • 1
  • 16
  • 35
0

I was also having the same problem running graphviz on the Datalab. Please run the following command before importing graphviz library in the Datalab notebook:

%%bash
/usr/bin/yes | apt-get install graphviz
pip install --upgrade graphviz
/usr/bin/yes | pip uninstall pyparsing
pip install -Iv https://pypi.python.org/packages/source/p/pyparsing/pyparsing-1.5.7.tar.gz
pip install --upgrade pydot

After that run the command and it will work well like this:

import graphviz
#visually representing the decision tree for num_leaves = 2
targetNames=['No Fault', 'Minor Fault','Major Fault']
dot_data = tree.export_graphviz(decTree, out_file= None,
                               feature_names=important_cols,
                               class_names = targetNames,
                               filled=True, rounded=True)
graph = graphviz.Source(dot_data)
graph

enter image description here

Gaurav Sitaula
  • 206
  • 1
  • 7