导出决策树的PDF

时间:2018-02-21 11:40:39

标签: python machine-learning scikit-learn

我正在使用scikitlearn向我介绍机器学习,我正在学习本教程link to yt 但如果我尝试导出pdf决策树,我有这个错误: 我尝试做:open -w review iris.pdf 结果是:

Impossibile ottenere un descrittore di file che si riferisce alla console

如果我从终端编译我有错误:

Traceback (most recent call last)  File "fstraining.py", line 2, in <module>
import graphviz ImportError: No module named graphviz

感谢您的关注

1 个答案:

答案 0 :(得分:0)

建立决策树clf之后,只需:

from sklearn.externals.six import StringIO   
from sklearn.tree import export_graphviz
import pydotplus

# Export resulting tree to DOT source code string
dot_data = export_graphviz(clf,
                           feature_names=col_names,
                           out_file=None,
                           filled=True,
                           rounded=True)

#Export to pdf
pydot_graph = pydotplus.graph_from_dot_data(dot_data)
pydot_graph.write_pdf('tree.pdf')

此答案改编自here