0

I am creating a decision tree using a dataset named as "wine": i am trying following code to execute:

dt = c.fit(X_train, y_train)

Creating the image of the decision tree:

where "Malik Shahid Ali" is the location/path of the image

def show_tree(tree, features, path):
    f = io.StringIO()
    export_graphviz(tree, out_file=f, feature_names=features)
    pydotplus.graph_from_dot_data(f.getvalue()).write_png("Malik Shahid Ali")
    img = misc.imread("Malik Shahid Ali")
    plt.imshow(img)

Calling the image:

show_tree(dt, features, 'dec_tree_01.png')

but when i call the image it gives the following error:

GraphViz's executables not found

import section:

import numpy as np
import pandas as pd
from sklearn import tree
from sklearn.tree import DecisionTreeClassifier, export_graphviz
from sklearn.model_selection import train_test_split
import graphviz
import pydotplus
import io
from scipy import misc
import matplotlib.pyplot as plt #sets up plotting under plt
import seaborn as sb
from pylab import rcParams

reading csv dataset

data=pd.read_csv('C:/Users/malik/Desktop/wine.csv',low_memory=False)
data.head()

train, test = train_test_split(data,test_size=0.15)

print("Training size: {} Test size: {}".format(len(train),len(test)))

c=DecisionTreeClassifier(min_samples_split=2)

features = ["id","Alcohol","Malic acid","Ash","Alcalinity of ash","Magnesium","Total phenols","Flavanoids","Field9Nonflavanoid phenols","Proanthocyanins","Color intensity","Hue","OD280/OD315 of diluted wines","Proline"]

X_train = train[features]
y_train = train["id"]

X_test = test[features]
y_test = test["id"]

y_test

dt = c.fit(X_train, y_train)

path of the excutable file:

import os     
os.environ["PATH"] += os.pathsep + 'E:\Graphviz2.38\bin'

image function:

def show_tree(tree, features, path):
    f = io.StringIO()
    export_graphviz(tree, out_file=f, feature_names=features)
    pydotplus.graph_from_dot_data(f.getvalue()).write_png(path)
    img = misc.imread(path)

    plt.imshow(img)

show_tree(dt, features, 'dec_tree_01.png')

Now on this command jupyter is giving eror like this:

E:\python\lib\site-packages\pydotplus\graphviz.py in create(self, prog, format)
   1958             if self.progs is None:
   1959                 raise InvocationException(
-> 1960                     'GraphViz\'s executables not found')
   1961 
   1962         if prog not in self.progs:

InvocationException: GraphViz's executables not found
Jack
  • 53
  • 1
  • 8

1 Answers1

0

I'm re-purposing my answer to a related problem here.

Make sure you have installed the actual executables, not just the python package. I used conda's install package here (recommended over pip install graphviz as pip install doesn't include the actual GraphViz executables).

Update

At the end of the day, an incorrectly formatted string path to the necessary file directory was added to the environment variable PATH. Be sure to add double back slashes in the string path to the directory, e.g.:

import os
os.environ["PATH"] += os.pathsep + 'E:\\Graphviz2.38\\bin\\' 
NickBraunagel
  • 1,559
  • 1
  • 16
  • 30
  • giving an error of URL while i am installing graphviz package – Jack Mar 26 '18 at 23:13
  • Moreover i have installed graphviz-2.38msi from there website...but the same error is continuously showing – Jack Mar 27 '18 at 00:29
  • Have you added the graphviz folder directory to your PATH environment variable? – NickBraunagel Mar 27 '18 at 11:05
  • yes i have added it like that %SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Graphviz2.38\bin; – Jack Mar 27 '18 at 15:15
  • @MalikMuhammadShahidAli There should be another folder within `bin` called `graphviz` (e.g. `graphviz-2.38.0-4\Library\bin\graphviz`) which needs to be added to `PATH`, as this is where the .exe files are located. Can you please try adding this folder directory to PATH? Overall, make sure you're adding the file directory which contains the .exe files (e.g. dot.exe). – NickBraunagel Mar 27 '18 at 19:18
  • i have done pip install graphviz...but its also not working...i am very much confused with the path... i have installed python on E drive...the graphviz folder was in C and i have paste it in E drive where my python setup originally exist... This also can not solve my issue. – Jack Mar 28 '18 at 01:56
  • Can you post your entire code, including the call to the wine dataset? – NickBraunagel Mar 28 '18 at 02:29
  • i have posted the complete code with an error. Have a look on it. – Jack Mar 28 '18 at 03:32
  • I think you need to add double back slashes when you add the graphviz folder directory to PATH: `os.environ["PATH"] += os.pathsep + 'E:\\Graphviz2.38\\bin\\'` – NickBraunagel Mar 28 '18 at 13:12