Visualizing decision tree in scikit-learn

后端 未结 11 2165
广开言路
广开言路 2020-12-02 10:26

I am trying to design a simple Decision Tree using scikit-learn in Python (I am using Anaconda\'s Ipython Notebook with Python 2.7.3 on Windows OS) and visualize it as follo

11条回答
  •  遥遥无期
    2020-12-02 11:10

    Here is the minimal code to have a nice looking graph with just 3 lines of code :

    from sklearn import tree
    import pydotplus
    
    dot_data=tree.export_graphviz(dt,filled=True,rounded=True)
    graph=pydotplus.graph_from_dot_data(dot_data)
    graph.write_png('tree.png')    
    plt.imshow(plt.imread('tree.png'))
    

    I have added the plt.imgshow to view the graph it Jupyter Notebook. You can ignore it if you are only interested in saving the png file.

    I installed the following dependencies:

    pip3 install graphviz
    pip3 install pydotplus
    

    For MacOs the pip version of Graphviz did not work. Following Graphviz's official documentation I installed it with brew and everything worked fine.

    brew install graphviz
    

提交回复
热议问题