how to extract decision rules (features splits) from xgboost model in python3 in python

You can extract the decision rules from an XGBoost model using the get_dump() method. Here's an example code:

main.py
import xgboost as xgb

# Train the XGBoost model
train_X = ...
train_y = ...
model = xgb.train(params, dtrain)

# Extract the decision rules
trees = model.get_dump()
for i, tree in enumerate(trees):
    print(f"Tree {i}:\n{tree}\n")
233 chars
12 lines

This will print out the decision rules for each tree in the XGBoost model. Each rule is represented as a string in the format of:

main.py
booster[<booster>]:
0:[f<feature_index> < threshold] yes=<left_child> no=<right_child>
1:...
...
97 chars
5 lines

Where <feature_index> is the index of the feature being split, <threshold> is the threshold value for the split, and <left_child> and <right_child> are the indices of the left and right child nodes respectively.

Note that the get_dump() method returns each tree as a string. You may need to parse the strings into a more convenient format for your usage.

gistlibby LogSnag