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

前端 未结 10 1786
深忆病人
深忆病人 2020-12-08 19:00

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 sa

相关标签:
10条回答
  • 2020-12-08 19:05

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

    0 讨论(0)
  • 2020-12-08 19:05

    I tried the previous answers and still got a error when running the script Therefore, I just used pydotplus

    import pydotplus
    

    and install the "graphviz" by using:

    sudo apt-get install graphviz
    

    Then it worked for me, and I added

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

    Thanks to the previous contributors.

    0 讨论(0)
  • 2020-12-08 19:05

    It works as the following on Python3.7 but don't forget to install pydot using Anaconda prompt:

       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')
    
    0 讨论(0)
  • 2020-12-08 19:14

    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") 
    
    0 讨论(0)
  • 2020-12-08 19:16

    @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.

    0 讨论(0)
  • 2020-12-08 19:16

    I use Anaconda. Here's what worked for me: run from terminal:

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

    Then run

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

    Then from the terminal:

    dot -Tpng tree.dot -o tree.png
    
    0 讨论(0)
提交回复
热议问题