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

26,320

Solution 1

pydot.graph_from_dot_data() returns a list, so try:

graph = pydot.graph_from_dot_data(dot_data.getvalue())
graph[0].write_pdf("iris.pdf") 

Solution 2

I think you are using newer version of python. Please try with pydotplus.

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

This should do it.

Solution 3

I had exactly the same issue. Turned out that I hadn't installed graphviz. Once i did that it started to work.

Solution 4

@Alex Sokolov, for my case in window, i downloaded and install / unzip the following to a folder then setup the PATH in Windows environment variables. re-run the py code works for me. hope is helpful to you.

Share:
26,320

Related videos on Youtube

乔守卿
Author by

乔守卿

Updated on July 09, 2022

Comments

  • 乔守卿
    乔守卿 almost 2 years

    My code is follow the class of machine learning of google.The two code are same.I don't know why it show error.May be the type of variable is error.But google's code is same to me.Who has ever had this problem?

    This is error

    [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]
    

    This is code

    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")
    
  • 乔守卿
    乔守卿 almost 8 years
    I just had a look.The graph is a list and it contain a pydot.Dot object.I use the pydot.Dot object call the function write_pdf("iris.pdf"),it also solve the question.
  • programmer
    programmer almost 8 years
    @乔守卿 What syntax did you use / how did you use pydot.Dot?
  • Weizhou He
    Weizhou He almost 8 years
    @programmer The length of this list is actually 1. So, just graph[0].write_pdf. BTW, I guess the reason it returns a list is that there might be multiple trees in a single dot file.
  • maheeka
    maheeka almost 7 years
    Thanks for the answer. Simple enough solution without having to go through the hassle of pydotplus
  • questionto42standswithUkraine
    questionto42standswithUkraine about 4 years
    I could only install with pip install pydotplus, not with conda install pydotplus which caused InvalidArchiveError('Error with archive C:\\Users\\Admin\\Anaconda3\\pkgs\\openssl-1.1.1d-he774522_2‌​xysboxfd\\pkg-openss‌​l-1.1.1d-he774522_2.‌​tar.zst. You probably need to delete and re-download or re-create this file. Message from libarchive was:\n\nCould not unlink')