在 Jupyter Notebook 中显示决策树时出错

时间:2021-02-25 12:01:28

标签: 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 = 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.

我还需要做什么?需要任何库或包吗?

2 个答案:

答案 0 :(得分:1)

你可以这样试试吗?

# 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)

enter image description here

有关如何执行此操作的更多想法,请参阅下面的链接。

https://github.com/ASH-WICUS/Notebooks/blob/master/Visualizing%20a%20Decision%20Tree%20-%20Classification%20and%20Regression.ipynb

答案 1 :(得分:0)

对于 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)

请查看我的这篇帖子了解更多信息:https://mljar.com/blog/visualize-decision-tree/