We can dump the number of trees in the xgboost model by below method:
Load the model and save it as a JSON file. Of course this step is not needed if the model was saved as a JSON already.
Then simply extract the num_trees information from the JSON file.
Below code snippet we may use for the same:
- import sys
- import json
- import xgboost as xgb
- if len(sys.argv) < 2:
- print(f'Usage: {sys.argv[0]} ')
- exit(1)
- loaded_model = xgb.Booster()
- loaded_model.load_model(sys.argv[1])
- loaded_model.save_model('/tmp/a_model.json')
- with open('/tmp/a_model.json', 'r') as fp:
- jsonrepr = json.load(fp)
- print(jsonrepr['learner']['gradient_booster']['model']['gbtree_model_param']['num_trees'])
We can run it as shown below (after saving the code snippet to a file xgb_tree_count.py): python xgb_tree_count.py model-file-path