【问题标题】:Attribute error using Pydot to create a graph使用 Pydot 创建图的属性错误
【发布时间】:2023-03-03 11:38:01
【问题描述】:

我在 Windows10 上使用 IPython 来训练和绘制决策树。我知道以下代码在一段时间前可以在 linux 上运行。我安装了 pydot 并且还安装了 graphviz(正确指定了路径)。

# Train and draw out a decision tree

from IPython import display
from sklearn import datasets, tree, utils
from sklearn.externals.six import StringIO  
import pydot 

# Train a small decision tree on the iris dataset
dataset = datasets.load_iris()
X_iris, y_iris = utils.shuffle(dataset.data, dataset.target,random_state=42)
tree_clf = tree.DecisionTreeClassifier(max_depth=3).fit(X_iris, y_iris)

# Generate a plot of the decision tree
dot_data = StringIO() 
tree.export_graphviz(tree_clf, out_file=dot_data) 
graph = pydot.graph_from_dot_data(dot_data.getvalue()) 
display.Image(graph.create_png())

我收到以下错误:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-5-3452aa5e9794> in <module>()
     15 tree.export_graphviz(tree_clf, out_file=dot_data)
     16 graph = pydot.graph_from_dot_data(dot_data.getvalue())
---> 17 display.Image(graph.create_png())

AttributeError: 'list' object has no attribute 'create_png'

【问题讨论】:

  • 请格式化您的输出以使其可读
  • 我已删除未格式化的文本 - 它只是 dot_data.getvalue() 中包含的值,此时可能不相关

标签: graphviz pydot


【解决方案1】:

我通过将所有pydot命令更改为pydotplus解决了这个问题。(包括import pydotplus)可能可以使用!pip install pydotplus安装pydotplus包。

参考 https://github.com/scikit-learn/scikit-learn/pull/7342/files

【讨论】:

  • 是的,在 python 3.5 中,这是解决问题的唯一方法。谢谢!
猜你喜欢
  • 2017-03-27
  • 1970-01-01
  • 2016-11-19
  • 2011-07-25
  • 1970-01-01
  • 2015-04-07
  • 2013-07-30
  • 2017-07-25
  • 1970-01-01
相关资源
最近更新 更多