Search code examples
pythonxgboostxgbclassifier

How to get hyperparameters of xgb.train in python


xgb.train is the low level API to train an xgboost model in Python.

  • When I use XGBClassifier, which is a wrapper and calls xgb.train when a model is trained, I can print the XGBClassifier object and the hyperparameters are printed.
  • When using xgb.train I have no idea how to check the parameters after training

Code:

bst = xgb.train(params, dtrain)
bst.params # does not work!

Solution

  • The save_config method noted here can be used to create a string representation of the model's configuration. This can be converted to a dict:

    import json
    
    config = json.loads(bst.save_config())
    

    The result is somewhat deeply nested, but the hyperparameters are found like this:

    config['learner']['gradient_booster']['updater']['grow_colmaker']['train_param']