如何保存 GridSearchCV xgboost 模型?

how to save GridSearchCV xgboost model?

我正在使用 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。 我该如何保存最佳模型本身?

试试这个:

# 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"))

你需要[best_estimator_]

Link here