使用RandomizedSearchCV调优XGBoost超参数

我正在尝试对一个包含约50万个观测值和10个特征的数据集使用XGBoost进行建模。我尝试使用RandomizedSearchCV进行超参数调优,但使用最佳参数的模型性能反而不如使用默认参数的模型好。

使用默认参数的模型:

model = XGBRegressor()model.fit(X_train,y_train["speed"])y_predict_speed = model.predict(X_test)from sklearn.metrics import r2_scoreprint("R2 score:", r2_score(y_test["speed"],y_predict_speed, multioutput='variance_weighted'))R2 score: 0.3540656307310167

随机搜索的最佳模型:

booster=['gbtree','gblinear']base_score=[0.25,0.5,0.75,1]## 超参数优化n_estimators = [100, 500, 900, 1100, 1500]max_depth = [2, 3, 5, 10, 15]booster=['gbtree','gblinear']learning_rate=[0.05,0.1,0.15,0.20]min_child_weight=[1,2,3,4]# 定义要搜索的超参数网格hyperparameter_grid = {    'n_estimators': n_estimators,    'max_depth':max_depth,    'learning_rate':learning_rate,    'min_child_weight':min_child_weight,    'booster':booster,    'base_score':base_score    }# 使用4折交叉验证设置随机搜索random_cv = RandomizedSearchCV(estimator=regressor,            param_distributions=hyperparameter_grid,            cv=5, n_iter=50,            scoring = 'neg_mean_absolute_error',n_jobs = 4,            verbose = 5,             return_train_score = True,            random_state=42)random_cv.fit(X_train,y_train["speed"])random_cv.best_estimator_XGBRegressor(base_score=0.5, booster='gblinear', colsample_bylevel=None,             colsample_bynode=None, colsample_bytree=None, gamma=None,             gpu_id=-1, importance_type='gain', interaction_constraints=None,             learning_rate=0.15, max_delta_step=None, max_depth=15,             min_child_weight=3, missing=nan, monotone_constraints=None,             n_estimators=500, n_jobs=16, num_parallel_tree=None,             random_state=0, reg_alpha=0, reg_lambda=0, scale_pos_weight=1,             subsample=None, tree_method=None, validate_parameters=1,             verbosity=None)

使用最佳模型:

regressor = XGBRegressor(base_score=0.5, booster='gblinear', colsample_bylevel=None,             colsample_bynode=None, colsample_bytree=None, gamma=None,             gpu_id=-1, importance_type='gain', interaction_constraints=None,             learning_rate=0.15, max_delta_step=None, max_depth=15,             min_child_weight=3, monotone_constraints=None,             n_estimators=500, n_jobs=16, num_parallel_tree=None,             random_state=0, reg_alpha=0, reg_lambda=0, scale_pos_weight=1,             subsample=None, tree_method=None, validate_parameters=1,             verbosity=None)regressor.fit(X_train,y_train["speed"])y_pred = regressor.predict(X_test)from sklearn.metrics import r2_scoreprint("R2 score:", r2_score(y_test["speed"],y_pred, multioutput='variance_weighted'))R2 score: 0.14258774171629718

如您所见,经过3小时的随机搜索后,准确率实际上下降了。如果我将线性模型改为树模型,数值会上升到0.65,那么为什么随机搜索不起作用呢?

我还收到了以下警告:

由于某些参数仅在语言绑定中使用但传递给了XGBoost核心,或者某些参数未使用但通过了此验证,这可能不准确。如果您发现上述情况,请提交问题报告。

关于这种超参数调优方法,有人有建议吗?


回答:

正如XGBoost文档中所述

参数调优是机器学习中的一门黑艺术,模型的最佳参数可能取决于许多场景。

您询问了针对您特定场景的建议,所以这里是我的几点建议。

  1. 从您的超参数搜索空间中删除booster维度。您可能希望使用默认的提升器’gbtree’。如果您对线性模型的性能感兴趣,您可以尝试线性回归岭回归,但在XGBoost参数调优期间不要考虑这些。
  2. 从您的超参数搜索空间中删除base_score维度。这在有足够多的提升迭代时应该不会有太大影响(参见XGB参数文档)。
  3. 目前您的网格中有3200种超参数组合。期望通过查看50个随机组合就能找到一个好的组合可能有点过于乐观。在删除boosterbase_score维度后,您将剩下
hyperparameter_grid = {    'n_estimators': [100, 500, 900, 1100, 1500],    'max_depth': [2, 3, 5, 10, 15],    'learning_rate': [0.05, 0.1, 0.15, 0.20],    'min_child_weight': [1, 2, 3, 4]    }

这有400种可能的组合。作为第一次尝试,我会进一步简化它。例如,您可以尝试如下设置:

hyperparameter_grid = {    'n_estimators': [100, 400, 800],    'max_depth': [3, 6, 9],    'learning_rate': [0.05, 0.1, 0.20],    'min_child_weight': [1, 10, 100]    }

只剩下81种组合,并且一些非常昂贵的组合(例如1500棵深度为15的树)被移除。当然,我不知道您的数据,所以也许有必要考虑如此大/复杂的模型。对于使用平方损失的回归任务,min_child_weight只是子节点中的实例数(再次参见XGB参数文档)。由于您有50万个观测值,叶节点中是否有1、2、3或4个观测值可能不会(或几乎不会)产生(太大)差异。因此,我在这里建议使用[1, 10, 100]。也许在这一网格中随机搜索能找到比默认参数更好的结果?

  1. 另一种策略可能是:为每种组合运行交叉验证
hyperparameter_grid = {    'max_depth': [3, 6, 9],    'min_child_weight': [1, 10, 100]    }

将学习率固定在某个常数值(不要太低,例如0.15)。对于每种设置,使用早期停止来确定适当的树的数量。这可以通过xgboost.cv方法的early_stopping_rounds参数实现。之后,您将知道max_depthmin_child_weight的一个好的组合(例如,对于给定问题,基础学习器需要多复杂?),以及对于此组合和固定学习率的一个好的树的数量。然后,微调可以包括在当前的(max_depth, min_child_weight)解决方案附近进行另一次超参数搜索,和/或在增加树的数量的同时降低学习率。

  1. 最后,由于回答有点长,如果详尽的网格搜索过于昂贵,还有随机搜索的其他替代方案。例如,您可以查看半网格搜索基于序列模型的优化

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注