How to save & load xgboost model?

后端 未结 5 433
南方客
南方客 2020-12-04 18:00

On the link of XGBoost guide:

After training, the model can be saved.

bst.save_model(\'0001.model\')

The model a

相关标签:
5条回答
  • 2020-12-04 18:08

    An easy way of saving and loading a xgboost model is with joblib library.

    import joblib
    #save model
    joblib.dump(xgb, filename) 
    
    #load saved model
    xgb = joblib.load(filename)
    
    0 讨论(0)
  • 2020-12-04 18:09

    Don't use pickle or joblib as that may introduces dependencies on xgboost version. The canonical way to save and restore models is by load_model and save_model.

    If you’d like to store or archive your model for long-term storage, use save_model (Python) and xgb.save (R).

    This is the relevant documentation for the latest versions of XGBoost. It also explains the difference between dump_model and save_model.

    Note that you can serialize/de-serialize your models as json by specifying json as the extension when using bst.save_model. If the speed of saving and restoring the model is not important for you, this is very convenient, as it allows you to do proper version control of the model since it's a simple text file.

    0 讨论(0)
  • 2020-12-04 18:17

    I found my way here because I was looking for a way to save and load my xgboost model. Here is how I solved my problem:

    import pickle
    file_name = "xgb_reg.pkl"
    
    # save
    pickle.dump(xgb_model, open(file_name, "wb"))
    
    # load
    xgb_model_loaded = pickle.load(open(file_name, "rb"))
    
    # test
    ind = 1
    test = X_val[ind]
    xgb_model_loaded.predict(test)[0] == xgb_model.predict(test)[0]
    
    Out[1]: True
    
    0 讨论(0)
  • 2020-12-04 18:23

    Both functions save_model and dump_model save the model, the difference is that in dump_model you can save feature name and save tree in text format.

    The load_model will work with model from save_model. The model from dump_model can be used for example with xgbfi.

    During loading the model, you need to specify the path where your models is saved. In the example bst.load_model("model.bin") model is loaded from file model.bin - it is just a name of file with model. Good luck!

    0 讨论(0)
  • 2020-12-04 18:25

    If you are using the sklearn api you can use the following:

    
    xgb_model_latest = xgboost.XGBClassifier() # or which ever sklearn booster you're are using
    
    xgb_model_latest.load_model("model.json") # or model.bin if you are using binary format and not the json
    

    If you used the above booster method for loading, you will get the xgboost booster within the python api not the sklearn booster in the sklearn api.

    So yeah, this seems to be the most pythonic way to load in a saved xgboost model data if you are using the sklearn api.

    0 讨论(0)
提交回复
热议问题