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

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

完美运行!!!

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值