How to save xgboost models using pipeline and GridSearchCV making it compatible with latter versions?

Viewed 359

I have a xgb classifier model which is trained using pipeline and GridSearchCV and I have saved it with pickle but this model was trained in older version of xgboost and now it is not compatible with new one. As per xgboost documentation if I would save xgboost model using save_model it would be compatible with later versions but in my case the saved object is a pipeline object so I can not save it as xgboost object. Is there any way to load this in new version without retraining model?

pipe = Pipeline(steps=[
    ('imputer', SimpleImputer(strategy='median')),
    ('scaler', StandardScaler())])

pipe_xgb = make_pipeline(pipe,DataFrameScaler(),xgb.XGBClassifier(objective='binary:logistic'))
param_grid_classifier = {
                        'xgbclassifier__n_estimators':[100,200], 
                         'xgbclassifier__learning_rate' :[0.1,0.01],
                         'xgbclassifier__colsample_bytree':[0.3,0.5],
                         'xgbclassifier__max_depth': [4],
                        'xgbclassifier__reg_lambda':[0.01,0.1],
                         'xgbclassifier__n_jobs':[4]
                        }
metric = 'average_precision'
grid_search1 = GridSearchCV(pipe_xgb, param_grid=param_grid_classifier,scoring=metric, cv=2)

gridmodel1 = grid_search1.fit(X_train, y_train_label)
model = gridmodel1.best_estimator_
# Save model
pickle.dump(model , open("xgb_log_reg.pickle", "wb"))
0 Answers
Related