1

Actually I'm using sklearn for visualizing decision tree. I have already installed graphviz and pydotplus and also set the environment variable of graphviz but when i run this code, it gives an error.

I'm using "kyphosis" dataset.

Here is the actual code

from IPython.display import Image
from sklearn.externals.six import StringIO
from sklearn.tree import export_graphviz
import pydotplus

features = list(df.columns[1:])
features

Output:
['Age', 'Number', 'Start']

it works fine

but when i run this below code. Code:

dot_data = StringIO()  
export_graphviz(dtree, 
out_file=dot_data,feature_names=features,filled=True,rounded=True)

graph = pydotplus.graph_from_dot_data(dot_data.getvalue())  
Image(graph[0].create_png())  

It shows an error.

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call 
last)
<ipython-input-44-28715c87781f> in <module>
      3 
      4 graph = pydotplus.graph_from_dot_data(dot_data.getvalue())
----> 5 Image(graph[0].create_png())

TypeError: 'Dot' object is not subscriptable

1 Answers1

0

Try to remove [0] from Image(graph[0].create_png()).

It shall work fine.

You'll be able to visualize your decision tree.

theletz
  • 1,713
  • 2
  • 16
  • 22