+2 votes
in Machine Learning by (74.2k points)
recategorized by
I want to save my trained XGboost model so that I can reuse it later because training takes several hours. How can I save the trained model and load it?

1 Answer

+1 vote
by (349k points)
selected by
 
Best answer

If you are using sklearn wrapper of XGboost, you can use pickle or joblib module.

Using pickle:

import pickle
bst = xgb.XGBClassifier(**param).fit(trainData.features, trainData.labels)
filename = 'global.model'

# to save the model
pickle.dump(bst, open(filename, 'wb'))

# to load the saved model
bst = pickle.load(open(filename, 'rb'))

Using Joblib:

import joblib
bst = xgb.XGBClassifier(**param).fit(trainData.features, trainData.labels)
filename = 'global.model'

# to save the model
joblib.dump(bst, open(filename, 'wb'))

# to load the saved model
bst = joblib.load(open(filename, 'rb'))

If you are using core XGboost, you can use functions save_model() and load_model() to save and load the model respectively.

dtrain = xgb.DMatrix(trainData.features,label=trainData.labels)              
bst = xgb.train(param, dtrain, num_boost_round=10)
filename = 'global.model'

# to save the model
bst.save_model(filename)

# to load the saved model
bst = xgb.Booster({'nthread':4})
bst.load_model(filename)


...