I\'m trying to visualize my DecisionTree, but getting the error The code is:
X = [i[1:] for i in dataset]#attribute
y = [i[0] for i in dataset]
clf = tree.Decisi
I had the same exact problem and just spent a couple hours trying to figure this out. I can't guarantee what I share here will work for others but it may be worth a shot.
pydot
packages but I have Python 3 and they simply did not work. After finding a note in a thread from one of the many websites I scoured through, I ended up installing this forked repository of pydot.Control Panel\All Control Panel Items\System\Advanced system settings
> click Environment Variables
button > under System variables
I found the variable path
> click Edit...
> I added ;C:\Program Files (x86)\Graphviz2.38\bin
to the end in the Variable value:
field.dot
commands in the Command Line (Windows Command Processor), I typed dot -V
which returned dot - graphviz version 2.38.0 (20140413.2041)
.In the below code, keep in mind that I'm reading a dataframe
from my clipboard. You might be reading it from file or whathaveyou.
In IPython Notebook:
import pandas as pd
import numpy as np
from sklearn import tree
import pydot
from IPython.display import Image
from sklearn.externals.six import StringIO
df = pd.read_clipboard()
X = df[df.columns[:-1]]
y = df[df.columns[-1]]
dtr = tree.DecisionTreeRegressor(max_depth=3)
dtr.fit(X, y)
dot_data = StringIO()
tree.export_graphviz(dtr, out_file=dot_data, feature_names=X.columns)
graph = pydot.graph_from_dot_data(dot_data.getvalue())
Image(graph.create_png())
Alternatively, if you're not using IPython, you can generate your own image from the command line as long as you have graphviz installed (step 2 above). Using my same example code above, you use this line after fitting the model:
tree.export_graphviz(dtr.tree_, out_file='treepic.dot', feature_names=X.columns)
then open up command prompt where the treepic.dot
file is and enter this command line:
dot -T png treepic.dot -o treepic.png
A .png file should be created with your decision tree.