How to find out internal logic of trained XGBoost model?

Suppose I have successfully trained a XGBoost machine learning model in python.

x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=7)
model = XGBClassifier()
model.fit(x_train, y_train)
y_pred = model.predict(x_test)

I want to port this model to another system which will be written in C/C++. To do this, I need to know the internal logic of the XGboost trained model and translate them into a series of if-then-else statements like decision trees, if I am not wrong.

How can this be done? How to find out the internal logic of the XGBoost trained model to implement it on another system?

I am using python 3.7.

From the XGBoost documents: "The model and its feature map can also be dumped to a text file.

dump model

bst.dump_model(‘dump.raw.txt’)

dump model with feature map

bst.dump_model(‘dump.raw.txt’, 'featmap.txt’)"

This will give you the if-else logic in text format.

1 Like

I am using XGBClassifier. It seems dump_model is not available in XGBClassifier. It is only available when you import xgboost as xgb.

How can I dump model with feature map with XGBClassifier?

Use XGBClassifier.get_booster() to get the Booster object first.

model.get_booster().dump_model('dump.txt')
1 Like

Thank you. I have tested that your solution works.