Search code examples
pythonxgboostdecision-tree

Retrieving values from decision trees leaves generated by XGBClassifier


I used the class XGBClassifier to build my model and then I visualized it in a tree as follows:

(...)
best_model = XGBClassifier(use_label_encoder=False,
                           eval_metric = 'logloss', 
                           learning_rate = 1, 
                           max_depth = 3,
                           n_estimators = 200)
(...)

from xgboost import plot_tree
import matplotlib.pyplot as plt
plot_tree(best_model,num_trees=0,rankdir='LR')

Picture here

Which, of course, plotted the best decision tree calculated by my classifier. My question is: how can I retrieve the values that are printed in leaves of my diagram? I believe they are stored in best_model but I don't know which method to use to get these values.

Thank you!


Solution

  • In Python you can dump the values to a string like this:

    m = xgb.XGBClassifier(max_depth=2, n_estimators=3).fit(X, y)
    m.get_booster().get_dump()
    

    or to a file:

    m.get_booster().dump_model("out.txt")
    

    or to a dataframe:

    m.get_booster().trees_to_dataframe()