从LightGBM模型中访问树和节点

10
在sci-kit learn中,可以访问整个树结构,即树的每个节点。这允许探索在树的每个分裂中使用的属性以及用于测试的哪些值。
The binary tree structure has 5 nodes and has the following tree structure:
node=0 test node: go to node 1 if X[:, 3] <= 0.800000011920929 else to node 2.
    node=1 leaf node.
    node=2 test node: go to node 3 if X[:, 2] <= 4.950000047683716 else to node 4.
            node=3 leaf node.
            node=4 leaf node.

Rules used to predict sample 0:
decision id node 0 : (X_test[0, 3] (= 2.4) > 0.800000011920929)
decision id node 2 : (X_test[0, 2] (= 5.1) > 4.950000047683716)

对于随机森林,您可以通过循环遍历所有决策树来获取相同的信息。

for tree in model.estimators_:
    # extract info from tree

LightGBM模型是否可以提取相同的信息?也就是说,您能否访问:a)每个树和b)每个树节点?

4个回答

7

是的,这可以通过

model._Booster.dump_model()["tree_info"]

该函数例如在lightgbm.plot_tree()中使用。但我必须承认,我自己没有使用过它,也不了解返回结构的详细信息。


4

1

还有model._Booster.num_trees()。它返回一个包含所有树的所有节点信息的Pandas DataFrame。

该DataFrame中包含的列的列表及其含义可以在官方文档中找到。


1
LightGBM几乎与XGBoost具有相同的功能;有时我甚至会去XGBoost文档中查找LightGBM的功能。您可以搜索如何在XGBoost中完成它,或者直接参考:https://github.com/Microsoft/LightGBM/issues/845 此外,LightGBM有一个sklearn包装器,可能可以将sklearn结构用于您训练的模型,就像您分享的方式一样。您可能想查看:https://lightgbm.readthedocs.io/en/latest/_modules/lightgbm/sklearn.html 希望我能帮到您,如果未解决,请不要犹豫写信给我; 我会深入了解详情。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接