首页 > 解决方案 > can not drawing decision tree with tree.export_text(clf)

问题描述

i wanna visualize my Decision Tree with the below code:

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

dot_data = StringIO()
export_graphviz(clf, out_file=dot_data,  
                filled=True, rounded=True,
                special_characters=True,feature_names = feature_cols,class_names=['0','1'])
graph = pydotplus.graph_from_dot_data(dot_data.getvalue())  
graph.write_png('diabetes.png')
Image(graph.create_png())

but I got this error:

NotFittedError: This Pipeline instance is not fitted yet. Call 'fit' with appropriate arguments before using this estimator.

In fact I fitted my model before with this code!

X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.2,
                                                    random_state=0)

clf.fit(X_train, y_train) 

标签: python-3.xscikit-learndecision-tree

解决方案


推荐阅读