guagay_wk
guagay_wk

Reputation: 28098

Dump XGBoost model with feature map using XGBClassifier

I would like to dump XGboost model and its feature map to a text file. This can be done something like this;

# https://xgboost.readthedocs.io/en/latest/python/python_intro.html
import xgboost as xgb
bst = xgb.train(param, dtrain, num_round, evallist)
# dump model with feature map
bst.dump_model('dump.raw.txt', 'featmap.txt')

However, I am using XGBClassifier. The method dump_model is not available in XGBClassifier.

from xgboost import XGBClassifier
xgboost_model = XGBClassifier()
xgboost_model.fit(x_train, y_train)
# line below can't work because dump_model is not available in XGBClassifier
xgboost_model.dump_model(‘dump.raw.txt’, 'featmap.txt’)  

How can I dump XGBoost model with feature map using XGBClassifier?

I am using python 3.7

Upvotes: 2

Views: 6172

Answers (1)

guagay_wk
guagay_wk

Reputation: 28098

I will answer my own question.

From https://xgboost.readthedocs.io/en/latest/python/python_api.html, use the get_booster() function first to get the underlying xgboost Booster of this model. The Booster has the dump_model function available.

bst = xgboost_model.get_booster()
bst.dump_model('dump.raw.txt', 'featmap.txt')

Upvotes: 7

Related Questions