# https://chrisalbon.com/machine_learning/trees_and_forests/visualize_a_decision_tree/
# Load libraries
from sklearn.tree import DecisionTreeClassifier
from sklearn import datasets
from IPython.display import Image
from sklearn import tree
# import pydotplus #
# I pasted this into Anaconda prompt conda install pydotplus
# the error persisted after I said pip install pydotplus
import pydotplus
# Load data
iris = datasets.load_iris()
X = iris.data
y = iris.target
# Train Decision Tree
# Create decision tree classifer object
clf = DecisionTreeClassifier(random_state=0)
# Train model
model = clf.fit(X, y)
# continuing https://chrisalbon.com/machine_learning/trees_and_forests/visualize_a_decision_tree/
# Visualize Decision Tree
# Create DOT data
dot_data = tree.export_graphviz(clf, out_file=None,
feature_names=iris.feature_names,
class_names=iris.target_names)
# Draw graph
graph = pydotplus.graph_from_dot_data(dot_data)
# Show graph
Image(graph.create_png())
在这一点上,我陷入了绝望:InvocationException Traceback(最后一次调用(在里面1011#显示图形--->12图像(graphic.create_png(((
~anaconda3libsite-packagespydotplusgraphviz.py in <lambda>(f, prog)
1795 self.__setattr__(
1796 'create_' + frmt,
-> 1797 lambda f=frmt, prog=self.prog: self.create(format=f, prog=prog)
1798 )
1799 f = self.__dict__['create_' + frmt]
~anaconda3libsite-packagespydotplusgraphviz.py in create(self, prog, format)
1957 self.progs = find_graphviz()
1958 if self.progs is None:
-> 1959 raise InvocationException(
1960 'GraphViz's executables not found')
1961
InvocationException: GraphViz's executables not found
我发现一些网站上说了一些关于更改路径的内容,但我不理解。我不是一名计算机科学家,我受过医学训练,但患有脑瘤,所以我学习了R并进行了临床试验研究。非常感谢您的帮助
在我的情况下,它在本地机器和Google Colab中都给出了输出。我建议制作一个单元格,并在jupyter笔记本中写入!pip install pydotplus
。谷歌Colab 链接
https://colab.research.google.com/drive/11d8kFgNc6Wm2Qawi_e7aSvUHc-SIfYAl?usp=sharing