Plot a Single XGBoost Decision Tree

前端 未结 4 1969
慢半拍i
慢半拍i 2021-01-05 04:13

I am using method on https://machinelearningmastery.com/visualize-gradient-boosting-decision-trees-xgboost-python/ to plot a XGBoost Decision Tree

from numpy         


        
相关标签:
4条回答
  • 2021-01-05 04:33

    To add to Serk's answer, you can also resize the figure before displaying it:

    # ...
    plot_tree(model)
    fig = plt.gcf()
    fig.set_size_inches(18.5, 10.5)
    plt.show()
    
    0 讨论(0)
  • 2021-01-05 04:35

    I had the same problem recently and the only way I found is by trying diffent figure size (it can still be bluery with big figure. For exemple, to plot the 4th tree, use:

    fig, ax = plt.subplots(figsize=(30, 30))
    xgb.plot_tree(model, num_trees=4, ax=ax)
    plt.show()
    

    To save it, you can do

    plt.savefig("temp.pdf")
    

    Also, each tree seperates two classes so you have as many tree as class.

    0 讨论(0)
  • 2021-01-05 04:35

    You can try using the to_graphviz method instead - for me it results in a much more clear picture.

    xgb.to_graphviz(xg_reg, num_trees=0, rankdir='LR')

    However, most likely you will have issues with the size of that output.

    In this case follow this: How can i specify the figsize of a graphviz representation of Decision Tree

    0 讨论(0)
  • 2021-01-05 04:45

    I found this workaround on github, which also gives better images with the drawback that you have to open the .png file after.

    xgb.plot_tree(bst, num_trees=2)
    fig = matplotlib.pyplot.gcf()
    fig.set_size_inches(150, 100)
    fig.savefig('tree.png')
    
    0 讨论(0)
提交回复
热议问题