0

i am having a problem producing a visual image of a decision tree. i am using python 2.7 and i have installed graphviz and pydot but when i run my code this is the error am getting "Could not run dot, ie graphviz, to produce visualization" what could be the problem?

NB am using windows not linux

Below is my code

from __future__ import print_function
import pandas as pd
import os
import subprocess
from sklearn.tree import DecisionTreeClassifier, export_graphviz
os.chdir('C:\\Users\\lussi\\Desktop\\tests')
df= pd.read_csv('yield2.csv')
print("* df.head()", df.head(), sep="\n", end="\n\n")
features = list(df.columns[:6])
y = df["yield"]
X = df[features]
dt = DecisionTreeClassifier(min_samples_split=20, random_state=99)
dt.fit(X, y)
def visualize_tree(tree, f_names):

with open("dt.dot", 'w') as f:
    export_graphviz(tree, out_file=f,
                    feature_names=f_names)

command = ["dot", "-Tpng", "dt.dot", "-o", "dt.png"]
try:
    subprocess.check_call(command)
except:
    exit("Could not run dot, ie graphviz, to "
         "produce visualization")
visualize_tree(dt, features) 
Rory Daulton
  • 21,934
  • 6
  • 42
  • 50
jkih
  • 1
  • 1
    Did you actually install the `dot` executable, and is it on your path? What happens when you run `dot -V` in the command shell? – PM 2Ring Jul 01 '16 at 10:03
  • 2
    If you are on windows, maybe the command should be `dot.exe` instead of `dot`? Just guessing... How about printing the actual Exception? – tobias_k Jul 01 '16 at 10:10
  • i used the pip install graphviz command to install the graphviz but when i issue the dot.exe -V command this is the error i get dot.exe : The term 'dot.exe' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At line:1 char:1 + dot.exe -V + ~~~~~~~ + CategoryInfo : ObjectNotFound: (dot.exe:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException – jkih Jul 11 '16 at 13:06

0 Answers0