graph.write_pdf("iris.pdf") AttributeError: 'list' object has no attribute 'write_pdf'
graph.write_pdf("iris.pdf") AttributeError: 'list' object has no attribute 'write_pdf'
我的代码是遵循 google.The 机器学习的 class 两个代码是 same.I 不知道为什么它显示 error.May 是变量的类型是error.But google的代码和me.Who一样 有没有遇到过这个问题?
这是错误
[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")
我认为您使用的是更新版本的 python。请尝试使用 pydotplus。
import pydotplus
...
graph = pydotplus.graph_from_dot_data(dot_data.getvalue())
graph.write_pdf("iris.pdf")
这应该可以做到。
pydot.graph_from_dot_data()
returns 一个列表,所以尝试:
graph = pydot.graph_from_dot_data(dot_data.getvalue())
graph[0].write_pdf("iris.pdf")
我通过 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')
最终在终端中转换为 png
dot -Tpng tree.dot -o tree.png
@Alex Sokolov,对于 window 中的案例,我下载并安装/解压缩 the following to a folder then setup the PATH in Windows environment variables。重新 运行 py 代码对我有用。希望对你有帮助。
我遇到了完全相同的问题。原来我没有安装graphviz。一旦我这样做了,它就开始工作了。
我尝试了前面的答案,但在 运行 脚本时仍然出现错误因此,
我刚刚使用了 pydotplus
import pydotplus
并使用以下命令安装“graphviz”:
sudo apt-get install graphviz
然后它对我有用,我添加了
graph = pydotplus.graph_from_dot_data(dot_data.getvalue())
graph.write_pdf("iris.pdf")
感谢之前的贡献者。
希望对您有所帮助,我也遇到了类似的问题。我决定不使用 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)
它在 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')
我使用 Anaconda。这是对我有用的:
运行 来自终端:
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
要添加您的 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")
它仍然有效。
我的代码是遵循 google.The 机器学习的 class 两个代码是 same.I 不知道为什么它显示 error.May 是变量的类型是error.But google的代码和me.Who一样 有没有遇到过这个问题?
这是错误
[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")
我认为您使用的是更新版本的 python。请尝试使用 pydotplus。
import pydotplus
...
graph = pydotplus.graph_from_dot_data(dot_data.getvalue())
graph.write_pdf("iris.pdf")
这应该可以做到。
pydot.graph_from_dot_data()
returns 一个列表,所以尝试:
graph = pydot.graph_from_dot_data(dot_data.getvalue())
graph[0].write_pdf("iris.pdf")
我通过 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')
最终在终端中转换为 png
dot -Tpng tree.dot -o tree.png
@Alex Sokolov,对于 window 中的案例,我下载并安装/解压缩 the following to a folder then setup the PATH in Windows environment variables。重新 运行 py 代码对我有用。希望对你有帮助。
我遇到了完全相同的问题。原来我没有安装graphviz。一旦我这样做了,它就开始工作了。
我尝试了前面的答案,但在 运行 脚本时仍然出现错误因此, 我刚刚使用了 pydotplus
import pydotplus
并使用以下命令安装“graphviz”:
sudo apt-get install graphviz
然后它对我有用,我添加了
graph = pydotplus.graph_from_dot_data(dot_data.getvalue())
graph.write_pdf("iris.pdf")
感谢之前的贡献者。
希望对您有所帮助,我也遇到了类似的问题。我决定不使用 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)
它在 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')
我使用 Anaconda。这是对我有用的: 运行 来自终端:
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
要添加您的 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")
它仍然有效。