如何保存GridSearchCV xgboost模型?

时间:2019-08-30 16:19:10

标签: python python-3.x scikit-learn pickle xgboost

我正在使用xgboost进行二进制分类。我正在使用GridSearchCV来找到最佳参数。但是,一旦发现具有最佳参数的模型,我不知道如何保存最佳模型。我该怎么做?

这是我的代码:

import xgboost as xgb
from sklearn.model_selection import StratifiedKFold, GridSearchCV

xgb_model = xgb.XGBClassifier(objective = "binary:logistic")

params = {
            'eta': np.arange(0.1, 0.26, 0.05),
            'min_child_weight': np.arange(1, 5, 0.5).tolist(),
            'gamma': [5],
            'subsample': np.arange(0.5, 1.0, 0.11).tolist(),
            'colsample_bytree': np.arange(0.5, 1.0, 0.11).tolist()
        }

scorers = {
            'f1_score':make_scorer(f1_score),
            'precision_score': make_scorer(precision_score),
            'recall_score': make_scorer(recall_score),
            'accuracy_score': make_scorer(accuracy_score)
          }

skf = StratifiedKFold(n_splits=10, shuffle = True)

grid = GridSearchCV(xgb_model, 
                    param_grid = params, 
                    scoring = scorers, 
                    n_jobs = -1, 
                    cv = skf.split(x_train, y_train),
                    refit = "accuracy_score")

grid.fit(x_train, y_train)
# Dictionary of best parameters
best_pars = grid.best_params_
# Save model
pickle.dump(grid.best_params_, open("xgb_log_reg.pickle", "wb"))

我虽然说#Save model 的那行是保存实际的最佳参数模型。但是,它只是保存字典 best_pars。。我该如何保存最佳模型本身?

1 个答案:

答案 0 :(得分:1)

尝试一下:

convBase

您需要# Dictionary of best parameters best_pars = grid.best_params_ # Best XGB model that was found based on the metric score you specify best_model = grid.best_estimator_ # Save model pickle.dump(grid.best_estimator_, open("xgb_log_reg.pickle", "wb"))

Link here