在 Jupyter Notebook 中显示决策树时出错
Posted
技术标签:
【中文标题】在 Jupyter Notebook 中显示决策树时出错【英文标题】:Error in Displaying Decision Tree in Jupyter Notebook 【发布时间】:2021-05-27 18:54:32 【问题描述】:我的代码:
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.
我还需要做什么?需要任何库或包吗?
【问题讨论】:
不是artificial-intelligence
问题,请不要发送垃圾邮件无关标签(已编辑)。
【参考方案1】:
对于 scikit-learn > 0.21,您可以在不使用其他软件包的情况下绘制树:
fig = plt.figure(figsize=(25,20))
_ = tree.plot_tree(clf,
feature_names=iris.feature_names,
class_names=iris.target_names,
filled=True)
【讨论】:
根据给定的代码,新错误显示:文件“你可以这样试试吗?
# 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
或者……
# 2
fig = plt.figure(figsize=(25,20))
_ = tree.plot_tree(clf,
feature_names=iris.feature_names,
class_names=iris.target_names,
filled=True)
有关如何执行此操作的更多想法,请参阅下面的链接。
https://github.com/ASH-WICUS/Notebooks/blob/master/Visualizing%20a%20Decision%20Tree%20-%20Classification%20and%20Regression.ipynb
【讨论】:
希望对您有所帮助!!以上是关于在 Jupyter Notebook 中显示决策树时出错的主要内容,如果未能解决你的问题,请参考以下文章
无法在 Jupyter Notebook 中显示 graphviz 树