如何保存GridSearchCV xgboost模型?

6

我正在使用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"))

我原本以为# 保存模型这行代码会将最佳参数下的实际模型进行保存。然而,它只是保存了字典best_pars。我该如何保存最佳模型本身呢?

1
您正在寻找best_estimator_属性,该属性基于refit参数提供最佳的XGB模型。 - G. Anderson
1个回答

2

试试这个:

# 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_]

点此链接

最初的回答

你好wundermahn,你如何使用XGBoost内部格式保存模型呢?这样可以方便在Python和Java之间进行移植。谢谢! https://xgboost.readthedocs.io/en/latest/python/python_api.html#xgboost.XGBRegressor.save_model - Aivoric

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接