python - graph.write_pdf ("iris.pdf") AttributeError : 'list' object has no attribute 'write_pdf'

标签 python machine-learning scikit-learn graphviz pydot

我的代码是按照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")

最佳答案

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

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

应该这样做。

关于python - graph.write_pdf ("iris.pdf") AttributeError : 'list' object has no attribute 'write_pdf' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38176472/

相关文章:

python - 正确使用 setBackground

python - 防止 pandas 在系列操作期间将 datetime.timedelta 强制为 numpy.timedelta64?

python - Pandas:将单元格拆分成多列,写入NaN

python - 如何判断哪个 Keras 模型更好?

machine-learning - 机器如何知道哪一步可以获得最大奖励?

machine-learning - FaceNet 傻瓜版

python - numpy/ Pandas : How to convert a series of strings of zeros and ones into a matrix

python - 如何在 python blessing/blessings/ncurses 中拥有 2 "areas"

python - 在 sklearn 或其他聚类库中进行聚类时,有没有办法强制将一组点分配给同一类?

python - 在 Python : statsmodel. api vs sklearn 中使用简单线性回归包的不同结果