简体   繁体   English

PYTHON 决策树可视化

[英]PYTHON Decision Tree Visualization

[![enter image description here][1]][1]I want to visualize tree decision classifier that i have applied to my data in pdf or png file. [![在此处输入图像描述][1]][1]我想可视化我已应用于 pdf 或 png 文件中的数据的树决策分类器。 I tried visualizing with graphviz via the code below:我尝试通过以下代码使用 graphviz 进行可视化:

X_train, X_test, y_train, y_test = \
        train_test_split(X, y, test_size=0.30, random_state=1)

clf =tree.DecisionTreeClassifier(max_depth=43)
clf = clf.fit(X_train, y_train)
from sklearn.externals.six import StringIO  
import pydot 
dot_data = StringIO() 
tree.export_graphviz(clf, out_file=dot_data) 
graph = pydot.graph_from_dot_data(dot_data.getvalue()) 
graph[0].write_pdf("tree.pdf") 

But the procedure can not be completed.但程序无法完成。 Once i got the error of running out of memory and for the second time i got the error "dot stop working".一旦我收到内存不足的错误,第二次我收到错误“点停止工作”。 Due to this issues i wanted to have an idea about the tree by knowing where is left children where is right children or left children?由于这个问题,我想通过知道左孩子在哪里,右孩子或左孩子在哪里来了解这棵树? thanks for any response and help感谢您的任何回应和帮助

If you are getting an error something like below:如果您收到类似以下内容的错误:

Program terminated with status: -11. stderr follows: dot: graph is too large for cairo-renderer bitmaps.

Then to understand the tree, you can try to put it in a tree text format on screen like below:然后为了理解树,您可以尝试将其以树文本格式显示在屏幕上,如下所示:

from sklearn.tree import export_text

r = export_text(clf, feature_names=df_X_train.columns)
print(r)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM