【问题标题】:Error trying to visualize scikit-learn Decision tree尝试可视化 scikit-learn 决策树时出错
【发布时间】:2020-09-08 06:23:24
【问题描述】:
import pandas as pd
import numpy as np

from sklearn.tree import DecisionTreeClassifier
from sklearn.tree import export_graphviz
from sklearn.externals.six import StringIO  

from IPython.display import Image  
import pydotplus

data_url = "https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data"
names=['sepal_l','sepal_w','petal_l','petal_w','class']
iris_df=pd.read_csv(data_url, names=names)

classes ={'Iris-setosa':0,'Iris-versicolor':1,'Iris-virginica': 2}
iris_df = iris_df.replace({'class': classes})

X = iris_df.iloc[:, :4]
Y = iris_df.iloc[:, 4]

tree = DecisionTreeClassifier(criterion='entropy')
tree.fit(X, Y)

dot_data = StringIO()
export_graphviz(tree, out_file=dot_data,filled=True,rounded=True,special_characters=True)
graph = pydotplus.graph_from_dot_data(dot_data.getvalue())  
Image(graph.create_png(), width=800, height=300)

输出:

InvocationException                       Traceback (most recent call last)
<ipython-input-6-f99e0713c908> in <module>
      4                 special_characters=True)
      5 graph = pydotplus.graph_from_dot_data(dot_data.getvalue())
----> 6 Image(graph.create_png(), width=800, height=300)

~\anaconda3\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]

~\anaconda3\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\Subba' is not recognized as an internal or external command,
operable program or batch file.

如何克服这个错误 我的是 Windows7-64 位 6GB RAM PC,python 3.8.0

【问题讨论】:

  • 从 sklearn.tree 尝试 plot_tree

标签: python machine-learning scikit-learn decision-tree


【解决方案1】:

试试这个,在call 附近尝试了几次后对我有用

export_graphviz(estimator, out_file='tree.dot', 
                feature_names = X_train.columns,
                class_names = da.unique(),
                rounded = True, proportion = False, 
                precision = 2, filled = True)

# Convert to png using system command (requires Graphviz)
from subprocess import call
call(['dot', '-Tpng', 'tree.dot', '-o', 'tree.png', '-Gdpi=600'])

# Display in jupyter notebook
from IPython.display import Image
Image(filename = 'tree.png')

【讨论】:

    猜你喜欢
    • 2015-03-05
    • 2012-05-21
    • 2015-03-18
    • 2017-02-23
    • 2020-04-05
    • 2017-07-21
    • 2022-08-24
    • 2017-03-26
    相关资源
    最近更新 更多