从 XGBClassifier 生成的决策树叶子中检索值

Retrieving values from decision trees leaves generated by XGBClassifier

我使用 class XGBClassifier 来构建我的模型,然后我将其可视化为树状结构,如下所示:

(...)
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

当然,它绘制了我的 classifier 计算出的最佳决策树。 我的问题是:如何检索图表叶子中打印的值?我相信它们存储在 best_model 中,但我不知道使用哪种方法来获取这些值。

谢谢!

在 Python 中,您可以将值转储为这样的字符串:

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

或文件:

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

或数据帧:

m.get_booster().trees_to_dataframe()