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_graphviz
returns a string and it does soHowever, there are more problems with your code. As
export_graphviz
returns a string, once you assign it toout
, you no longer have aStringIO
object, but anstr
object. To save the returned value to aStringIO
object, do not provideout_file
and save toout
as follows:Since you write
from StringIO import StringIO
I am assumming you are using Python 2.x.