《机器学习之路》代码问题解决二

打印决策树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

完美运行!!!

发布了9 篇原创文章 · 获赞 2 · 访问量 3089

猜你喜欢

转载自blog.csdn.net/xiaokan_001/article/details/87882283