从XGBClassifier产生的决策树中检索值

发布于 2025-01-20 08:23:19 字数 669 浏览 3 评论 0原文

我使用类 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')

图片在这里

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

谢谢你!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忱杏 2025-01-27 08:23:19

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

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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文