Titus Pullo
Titus Pullo

Reputation: 3821

Access trees and nodes from LightGBM model

In sci-kit learn, it's possible to access the entire tree structure, that is, each node of the tree. This allows to explore the attributes used at each split of the tree and which values are used for the test

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 the Random Forest, you can obtain the same information by looping across all the decision trees

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

Can the same information be extracted from a LightGBM model? That is, can you access: a) every tree and b) every node of a tree?

Upvotes: 10

Views: 10547

Answers (3)

Robert Robison
Robert Robison

Reputation: 389

model.booster_.trees_to_dataframe() returns a pandas DataFrame with one row per split per tree with information such as the split feature, split value, gain, and record count.

See documentation here: https://lightgbm.readthedocs.io/en/latest/pythonapi/lightgbm.Booster.html#lightgbm.Booster.trees_to_dataframe

Upvotes: 5

Joel
Joel

Reputation: 15742

Yes, this is possible with

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

which is for example used in lightgbm.plot_tree(). I must admit though that I haven't used it myself and don't know the details about the returned structure.

Upvotes: 7

Ugur MULUK
Ugur MULUK

Reputation: 464

LightGBM has almost the same functions with XGBoost; sometimes I even go to the XGBoost documentation to find the functions of LightGBM. You can search for how it is done in XGBoost or you can refer directly to: https://github.com/Microsoft/LightGBM/issues/845

Also, LightGBM has a sklearn wrapper, it is probably possible to use the sklearn structure on the model you train as the way you shared. You may want to have a look at: https://lightgbm.readthedocs.io/en/latest/_modules/lightgbm/sklearn.html

Hope I could help, please do not hesitate to write if not resolved; I will go deeper in details.

Upvotes: 1

Related Questions