0

I am trying to visiualize my decisiontree.Below is the code which I have tried

 from StringIO import StringIO
 from sklearn import tree
 out = StringIO()
 clf =DecisionTreeClassifier(X,y)
 out = tree.export_graphviz(clf, out_file=out)
 print out.getvalue()

Below is the error which i am getting

AttributeError                            Traceback (most recent call last)
<ipython-input-33-7b068216688f> in <module>()
  4 
  5 out = tree.export_graphviz(clf, out_file=out)
 ---->  6 print out.getvalue()

 AttributeError: 'NoneType' object has no attribute 'getvalue'

How do I solve this?

swetha
  • 131
  • 4
  • 15
  • see this: http://stackoverflow.com/questions/27817994/visualizing-decision-tree-in-scikit-learn – M.M Dec 21 '16 at 11:49

1 Answers1

0

The sklearn documentation states that export_graphviz returns a string and it does so

only if out_file is None

However, there are more problems with your code. As export_graphviz returns a string, once you assign it to out, you no longer have a StringIO object, but an str object. To save the returned value to a StringIO object, do not provide out_file and save to out as follows:

print >>out, tree.export_graphviz(clf)

Since you write from StringIO import StringIO I am assumming you are using Python 2.x.

ergys
  • 1,109
  • 11
  • 20