打印决策树png图片代码
按照书上原代码执行,出错后,搜索一番,发现因为Python的更新,用法改变
原代码为:
titanic.estimator.decision_tree_classifier(criterion='entropy',max_depth=3)
clf = titanic.fit()
dotfile = StringIO()
tree.export_graphviz(clf,out_file=dotfile,feature_names=titanic.df.columns[1:])
pydot.graph_from_dot_data(dotfile.getvalue()).write_png('dtree.png')
!open dtree.png
解决方案一:
titanic.estimator.decision_tree_classifier(criterion='entropy',max_depth=3)
clf = titanic.fit()
dotfile = StringIO()
tree.export_graphviz(clf,out_file=dotfile,feature_names=titanic.df.columns[1:])
graph = pydot.graph_from_dot_data(dotfile.getvalue())
graph[0].write_png('dtree.png')
!open dtree.png
解决方法二:安装pydotplus模块
import pydotplus
titanic.estimator.decision_tree_classifier(criterion='entropy',max_depth=3)
clf = titanic.fit()
dotfile = StringIO()
tree.export_graphviz(clf,out_file=dotfile,feature_names=titanic.df.columns[1:])
pydot.graph_from_dot_data(dotfile.getvalue()).write_png('dtree.png')
!open dtree.png
完美运行!!!