首页 > 解决方案 > Pydotplus,Graphviz 错误:程序以状态终止:1. 标准错误如下:'C:\Users\En' 未被识别为内部或外部命令

问题描述

from pydotplus import graph_from_dot_data
from sklearn.tree import export_graphviz
from IPython.display import Image 

dot_data = export_graphviz(tree,filled=True,rounded=True,class_names=['Setosa','Versicolor','Virginica'],feature_names=['petal length','petal width'],out_file=None)
graph = graph_from_dot_data(dot_data)
Image(graph.create_png())

程序以状态终止:

 1. stderr follows: 'C:\Users\En' is not recognized as an internal or external command,
operable program or batch file.

似乎它将我的用户名分成两半。我该如何克服这个问题?

标签: graphvizpydotplus

解决方案


我有一个非常相似的示例,我正在尝试,它基于 ML 操作手册,该手册与预测违约风险的台湾信用卡数据集一起使用。我的设置如下:

from six import StringIO
from sklearn.tree import export_graphviz
from IPython.display import Image 
import pydotplus

然后以这种方式创建决策树图:

dot_data = StringIO()
export_graphviz(decision_tree=class_tree,
                out_file=dot_data,
                filled=True,
                rounded=True,
                feature_names = X_train.columns,
                class_names = ['pay','default'],
                special_characters=True)
graph = pydotplus.graph_from_dot_data(dot_data.getvalue()) 
Image(graph.create_png())

我认为这一切都来自out_file=dot_data参数,但无法确定文件路径的创建和存储位置,因为print(dot_data.getvalue())没有显示任何路径名。

在我的研究中,我遇到了 sklearn.plot_tree() ,它似乎可以完成 graphviz 所做的所有事情。所以我采用了上面的 exporet_graphviz 参数,并且匹配的参数在我添加的 .plot_tree 方法中。

我最终得到了以下内容,它创建了与文本中相同的图像:

from sklearn import tree

plt.figure(figsize=(20, 10))
tree.plot_tree(class_tree, 
               filled=True, rounded=True, 
               feature_names = X_train.columns,
               class_names = ['pay','default'],
               fontsize=12)
plt.show()

推荐阅读