Jan Sila
Jan Sila

Reputation: 1593

Plotting decision tree, graphvizm pydotplus

I'm following the tutorial for decision tree on scikit documentation. I have pydotplus 2.0.2 but it is telling me that it does not have write method - error below. I've been struggling for a while with it now, any ideas, please? Many thanks!

from sklearn import tree
from sklearn.datasets import load_iris

iris = load_iris()
clf = tree.DecisionTreeClassifier()
clf = clf.fit(iris.data, iris.target)

from IPython.display import Image

dot_data = tree.export_graphviz(clf, out_file=None)
import pydotplus

graph = pydotplus.graphviz.graph_from_dot_data(dot_data)

Image(graph.create_png())

and my error is

    /Users/air/anaconda/bin/python /Users/air/PycharmProjects/kiwi/hemr.py
Traceback (most recent call last):
  File "/Users/air/PycharmProjects/kiwi/hemr.py", line 10, in <module>
    dot_data = tree.export_graphviz(clf, out_file=None)
  File "/Users/air/anaconda/lib/python2.7/site-packages/sklearn/tree/export.py", line 375, in export_graphviz
    out_file.write('digraph Tree {\n')
AttributeError: 'NoneType' object has no attribute 'write'

Process finished with exit code 1

----- UPDATE -----

Using the fix with out_file, it throws another error:

 Traceback (most recent call last):
  File "/Users/air/PycharmProjects/kiwi/hemr.py", line 13, in <module>
    graph = pydotplus.graphviz.graph_from_dot_data(dot_data)
  File "/Users/air/anaconda/lib/python2.7/site-packages/pydotplus/graphviz.py", line 302, in graph_from_dot_data
    return parser.parse_dot_data(data)
  File "/Users/air/anaconda/lib/python2.7/site-packages/pydotplus/parser.py", line 548, in parse_dot_data
    if data.startswith(codecs.BOM_UTF8):
AttributeError: 'NoneType' object has no attribute 'startswith'

---- UPDATE 2 -----

Also, se my own answer below which solves another problem

Upvotes: 7

Views: 23726

Answers (6)

gourab ghosh
gourab ghosh

Reputation: 169

i would suggest avoid graphviz & use the following alternate approach

from sklearn.tree import plot_tree
plt.figure(figsize=(60,30))
plot_tree(clf, filled=True);

Upvotes: 0

MMF
MMF

Reputation: 5921

The problem is that you are setting the parameter out_file to None.

If you look at the documentation, if you set it at None it returns the string file directly and does not create a file. And of course a string does not have a write method.

Therefore, do as follows :

dot_data = tree.export_graphviz(clf)
graph = pydotplus.graphviz.graph_from_dot_data(dot_data)

Upvotes: 7

K.S.
K.S.

Reputation: 9

What really helped me solve the problem was:- I executed the code from the same user through which graphviz was installed. So executing from any other user would give your error

Upvotes: 0

zongyan
zongyan

Reputation: 71

I met the same error this morning. I use python 3.x and here is how I solve the problem.

from sklearn import tree
from sklearn.datasets import load_iris
from IPython.display import Image
import io

iris = load_iris()
clf = tree.DecisionTreeClassifier()
clf = clf.fit(iris.data, iris.target)

# Let's give dot_data some space so it will not feel nervous any more
dot_data = io.StringIO()
tree.export_graphviz(clf, out_file=dot_data)
import pydotplus

graph = pydotplus.graphviz.graph_from_dot_data(dot_data.getvalue())
# make sure you have graphviz installed and set in path
Image(graph.create_png())

if you use python 2.x, I believe you need to change "import io" as:

import StringIO

and,

dot_data = StringIO.StringIO()

Hope it helps.

Upvotes: 2

Jan Sila
Jan Sila

Reputation: 1593

Also another problem was the backend settings to my Graphviz!! It is solved nicely here. you just need to lookup that settings file and change backend, or in the code mpl.use("TkAgg") as suggested there in the comments. After I only got error that pydotplot couldn't find my Graphviz executable, hence I reinstalled Graphviz via homebrew: brew install graphviz which solved the issue and I can make plots now!!

Upvotes: 0

Vadym  Pasko
Vadym Pasko

Reputation: 349

Method graph_from_dot_data() didn't work for me even after specifying proper path for out_file.

Instead try using graph_from_dot_file method:

graph = pydotplus.graphviz.graph_from_dot_file("iris.dot")

Upvotes: 3

Related Questions