我的代码是按照谷歌的机器学习类。两个代码是相同的。我不知道为什么它显示错误。可能是变量的类型是错误。但谷歌的代码对我来说是相同的。谁有有这个问题吗?
这是错误
[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")
答案 0 :(得分:62)
我认为您使用的是较新版本的python。请尝试使用pydotplus。
import pydotplus
...
graph = pydotplus.graph_from_dot_data(dot_data.getvalue())
graph.write_pdf("iris.pdf")
这应该这样做。
答案 1 :(得分:23)
pydot.graph_from_dot_data()
会返回一个列表,请尝试:
graph = pydot.graph_from_dot_data(dot_data.getvalue())
graph[0].write_pdf("iris.pdf")
答案 2 :(得分:5)
我有完全相同的问题。原来我没有安装graphviz。一旦我这样做,它开始工作。
答案 3 :(得分:1)
@Alex Sokolov,对于我在窗口中的情况,我下载并安装/解压缩the following到一个文件夹,然后设置PATH in Windows environment variables。重新运行py代码对我有用。希望对你有所帮助。
答案 4 :(得分:0)
我通过conda安装scikit-learn,所有这些都无法正常工作。 首先,我必须安装libtool
brew install libtool --universal
然后我关注this sklearn guide 然后将python文件更改为此代码
clf = clf.fit(train_data, train_target)
tree.export_graphviz(clf,out_file='tree.dot')
最后转换为终端
中的pngdot -Tpng tree.dot -o tree.png
答案 5 :(得分:0)
我尝试了以前的答案,但在运行脚本时仍然出错,因此, 我刚用 pydotplus
import pydotplus
并使用以下命令安装“ graphviz ”:
sudo apt-get install graphviz
然后它对我有用,我添加了
graph = pydotplus.graph_from_dot_data(dot_data.getvalue())
graph.write_pdf("iris.pdf")
感谢以前的贡献者。
答案 6 :(得分: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')
答案 7 :(得分: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
答案 8 :(得分:0)
要为n_estimator的数量添加所有图形,可以执行以下操作:
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")
它仍然可以工作。
答案 9 :(得分:-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)