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?
The sklearn documentation states that
export_graphvizreturns a string and it does soHowever, there are more problems with your code. As
export_graphvizreturns a string, once you assign it toout, you no longer have aStringIOobject, but anstrobject. To save the returned value to aStringIOobject, do not provideout_fileand save tooutas follows:Since you write
from StringIO import StringIOI am assumming you are using Python 2.x.