46

我的代码是按照google的机器学习类的。两个代码是一样的。我不知道为什么会显示错误。可能是变量的类型是错误的。但是google的代码和我一样。谁有过这个问题?

这是错误

[0 1 2]
[0 1 2]
Traceback (most recent call last):
  File "/media/joyce/oreo/python/machine_learn/VisualizingADecisionTree.py", line 34, in <module>
    graph.write_pdf("iris.pdf")
AttributeError: 'list' object has no attribute 'write_pdf'
[Finished in 0.4s with exit code 1]
[shell_cmd: python -u "/media/joyce/oreo/python/machine_learn/VisualizingADecisionTree.py"]
[dir: /media/joyce/oreo/python/machine_learn]
[path: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games]

这是代码

import numpy as np
from sklearn.datasets import load_iris
from sklearn import tree

iris = load_iris()
test_idx = [0, 50, 100]

# training data
train_target = np.delete(iris.target, test_idx)
train_data = np.delete(iris.data, test_idx, axis=0)

# testing data
test_target = iris.target[test_idx]
test_data = iris.data[test_idx]

clf = tree.DecisionTreeClassifier()
clf.fit(train_data, train_target)

print test_target
print clf.predict(test_data) 

# viz code
from sklearn.externals.six import StringIO
import pydot
dot_data = StringIO()
tree.export_graphviz(clf,
        out_file=dot_data,
        feature_names=iris.feature_names,
        class_names=iris.target_names,
        filled=True, rounded=True,
        impurity=False)

graph = pydot.graph_from_dot_data(dot_data.getvalue())
graph.write_pdf("iris.pdf")
4

10 回答 10

69

我认为您使用的是较新版本的python。请尝试使用 pydotplus。

import pydotplus
...
graph = pydotplus.graph_from_dot_data(dot_data.getvalue())
graph.write_pdf("iris.pdf")

这应该这样做。

于 2016-07-04T04:59:49.480 回答
29

pydot.graph_from_dot_data()返回一个列表,所以尝试:

graph = pydot.graph_from_dot_data(dot_data.getvalue())
graph[0].write_pdf("iris.pdf") 
于 2016-08-26T15:15:16.663 回答
5

我有完全相同的问题。原来我没有安装graphviz。一旦我这样做了,它就开始工作了。

于 2017-07-02T04:05:51.600 回答
1

@Alex Sokolov,对于我在窗口中的情况,我下载并将以下内容安装/解压缩到一个文件夹,然后在 Windows 环境变量中设置 PATH。重新运行 py 代码对我有用。希望对你有帮助。

于 2017-05-15T08:41:14.267 回答
0

我通过 conda 安装了 scikit-learn,但所有这些都不起作用。首先,我必须安装 libtool

brew install libtool --universal

然后我按照这个sklearn指南 然后将python文件更改为这段代码

clf = clf.fit(train_data, train_target)
tree.export_graphviz(clf,out_file='tree.dot') 

最后在终端转换为png

dot -Tpng tree.dot -o tree.png
于 2016-10-19T03:13:22.537 回答
0

我尝试了前面的答案,运行脚本时仍然报错因此,我只是使用了pydotplus

import pydotplus

并使用以下方法安装“ graphviz ”:

sudo apt-get install graphviz

然后它对我有用,我添加了

graph = pydotplus.graph_from_dot_data(dot_data.getvalue())
graph.write_pdf("iris.pdf")

感谢之前的贡献者。

于 2018-03-20T08:53:03.550 回答
0

它在 Python3.7 上的工作方式如下,但不要忘记使用 Anaconda 提示符安装 pydot:

   from sklearn.externals.six import StringIO
   import pydot

   # viz code
   dot_data = StringIO()
   tree.export_graphviz(clf, out_file=dot_data, feature_names=iris.feature_names,
                 class_names=iris.target_names, filled=True, rounded=True,
                 impurity=False)
   graph = pydot.graph_from_dot_data(dot_data.getvalue())
   graph[0].write_pdf('iris.pdf')
于 2018-12-11T09:25:30.567 回答
0

我用蟒蛇。这对我有用:从终端运行:

conda install python-graphviz
conda install pydot     ## don't forget this <-----------------

然后运行

clf = clf.fit(train_data, train_target)
tree.export_graphviz(clf,out_file='tree.dot')

然后从终端:

dot -Tpng tree.dot -o tree.png
于 2019-06-05T23:11:24.197 回答
0

要为您的 n_estimators 数量添加所有图表,您可以执行以下操作:

for i in range(0, n):  #n is your n_estimators number
    dot_data = StringIO()
    tree.export_graphviz(clf.estimators_[i], out_file=dot_data, feature_names=iris.feature_names,
                        class_names=iris.target_names, filled=True, rounded=True,
                        impurity=False)
    graph = pydotplus.graph_from_dot_data(dot_data.getvalue())
    graph.write_pdf("iris%s.pdf"%i)

你也可以换线

graph = pydotplus.graph_from_dot_data(dot_data.getvalue())

对于这个

(graph,) = pydot.graph_from_dot_data(dot_data.getvalue())
graph.write_pdf("iris.pdf")

它仍然可以工作。

于 2019-10-30T10:54:02.207 回答
-1

我希望这会有所帮助,我遇到了类似的问题。我决定不使用 pydot / pydotplus,而是使用graphviz。我(几乎没有)修改了代码,它创造了奇迹!:)

# 2. Train classifier
# Testing Data
# Examples used to "test" the classifier's accuracy
# Not part of the training data
import numpy as np
from sklearn.datasets import load_iris
from sklearn import tree
iris = load_iris()
test_idx = [0, 50, 100] # Grabs one example of each flower for testing data (in the data set it so happens to be that
                        # each flower begins at 0, 50, and 100

# training data
train_target = np.delete(iris.target, test_idx)     # Delete all but 3 for training target data
train_data = np.delete(iris.data, test_idx, axis=0) # Delete all but 3 for training data

# testing data
test_target = iris.target[test_idx] # Get testing target data
test_data = iris.data[test_idx]     # Get testing data

# create decision tree classifier and train in it on the testing data
clf = tree.DecisionTreeClassifier()
clf.fit(train_data, train_target)

# Predict label for new flower
print(test_target)
print(clf.predict(test_data))

# Visualize the tree
from sklearn.externals.six import StringIO
import graphviz
dot_data = StringIO()
tree.export_graphviz(clf,
        out_file=dot_data,
        feature_names=iris.feature_names,
        class_names=iris.target_names,
        filled=True, rounded=True,
        impurity=False)
graph = graphviz.Source(dot_data.getvalue())
graph.render("iris.pdf", view=True)
于 2018-05-19T05:05:15.293 回答