Python Jupyter笔记本中显示决策树时出错
我的代码:Python Jupyter笔记本中显示决策树时出错,python,scikit-learn,decision-tree,pydot,pydotplus,Python,Scikit Learn,Decision Tree,Pydot,Pydotplus,我的代码: from IPython.display import Image from sklearn.externals.six import StringIO import pydotplus dot_data = StringIO() tree.export_graphviz(clf, out_file=dot_data, feature_names=list(features.columns.values)) graph =
from IPython.display import Image
from sklearn.externals.six import StringIO
import pydotplus
dot_data = StringIO()
tree.export_graphviz(clf, out_file=dot_data,
feature_names=list(features.columns.values))
graph = pydotplus.graph_from_dot_data(dot_data.getvalue())
Image(graph.create_png())
错误:
---------------------------------------------------------------------------
InvocationException Traceback (most recent call last)
<ipython-input-11-35d87411c12d> in <module>
7 feature_names=list(features.columns.values))
8 graph = pydotplus.graph_from_dot_data(dot_data.getvalue())
----> 9 Image(graph.create_png())
~\.conda\envs\myTensorflow\lib\site-packages\pydotplus\graphviz.py in <lambda>(f, prog)
1789 self.__setattr__(
1790 'create_' + frmt,
-> 1791 lambda f=frmt, prog=self.prog: self.create(format=f, prog=prog)
1792 )
1793 f = self.__dict__['create_' + frmt]
~\.conda\envs\myTensorflow\lib\site-packages\pydotplus\graphviz.py in create(self, prog, format)
2024 raise InvocationException(
2025 'Program terminated with status: %d. stderr follows: %s' % (
-> 2026 status, stderr_output))
2027 elif stderr_output:
2028 print(stderr_output)
InvocationException: Program terminated with status: 1. stderr follows: 'C:\Users\NEO' is not recognized as an internal or external command,
operable program or batch file.
---------------------------------------------------------------------------
调用异常回溯(最近一次调用上次)
在里面
7要素名称=列表(要素.列.值))
8 graph=pydotplus.graph_from_dot_data(dot_data.getvalue())
---->9图像(graph.create_png())
(f,prog)中的~\.conda\envs\myTensorflow\lib\site packages\pydotplus\graphviz.py
1789年自__(
1790“创建”+frmt,
->1791 lambda f=frmt,prog=self.prog:self.create(format=f,prog=prog)
1792 )
1793 f=自我.uuuu指令uuuuu['create_uuu'+frmt]
create中的~\.conda\envs\myTensorflow\lib\site packages\pydotplus\graphviz.py(self、prog、format)
2024年引发的异常(
2025'程序已终止,状态为:%d。标准如下:%s'%(
->2026年状态,标准输出)
2027年elif标准输出:
2028打印(标准输出)
调用异常:程序已终止,状态为1。stderr如下:“C:\Users\NEO”不被识别为内部或外部命令,
可操作的程序或批处理文件。
我还需要做什么?是否需要任何库或软件包?对于scikit learn>0.21,您可以在不使用其他软件包的情况下绘制树:
fig=plt.figure(figsize=(25,20))
_=树。绘图树(clf,
功能名称=iris.feature名称,
class_name=iris.target_name,
填充=真)
请查看我的这篇帖子了解更多信息:你能像这样试试吗
# Visualizing a Decision Tree using a Classifier (discrete variables, labels, etc.)
from matplotlib import pyplot as plt
from sklearn import datasets
from sklearn.tree import DecisionTreeClassifier
from sklearn import tree
# Prepare the data data
iris = datasets.load_iris()
X = iris.data
y = iris.target
# Fit the classifier with default hyper-parameters
clf = DecisionTreeClassifier(random_state=1234)
model = clf.fit(X, y)
# 1
text_representation = tree.export_text(clf)
print(text_representation)
# if you want to save the tree...
with open("decistion_tree.log", "w") as fout:
fout.write(text_representation)
结果:
|--- feature_2 <= 2.45
| |--- class: 0
|--- feature_2 > 2.45
| |--- feature_3 <= 1.75
| | |--- feature_2 <= 4.95
| | | |--- feature_3 <= 1.65
| | | | |--- class: 1
| | | |--- feature_3 > 1.65
| | | | |--- class: 2
| | |--- feature_2 > 4.95
| | | |--- feature_3 <= 1.55
| | | | |--- class: 2
| | | |--- feature_3 > 1.55
| | | | |--- feature_0 <= 6.95
| | | | | |--- class: 1
| | | | |--- feature_0 > 6.95
| | | | | |--- class: 2
| |--- feature_3 > 1.75
| | |--- feature_2 <= 4.85
| | | |--- feature_1 <= 3.10
| | | | |--- class: 2
| | | |--- feature_1 > 3.10
| | | | |--- class: 1
| | |--- feature_2 > 4.85
| | | |--- class: 2
请参阅下面的链接,以了解更多有关如何执行此操作的想法
这不是一个
人工智能问题,请不要发送不相关的标签(已编辑)。根据给定的代码,新错误显示:文件“”,第5行fig=plt.figure(figsize=(25,20))\u=tree.plot\u tree(clf,feature\u names=iris.feature\u names,class\u names=iris.target\u names,filled=True)^SyntaxError:无效的syntaxthank you ASH Brother它有帮助!!
# 2
fig = plt.figure(figsize=(25,20))
_ = tree.plot_tree(clf,
feature_names=iris.feature_names,
class_names=iris.target_names,
filled=True)