我正在尝试对一个包含约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文档中所述
参数调优是机器学习中的一门黑艺术,模型的最佳参数可能取决于许多场景。
您询问了针对您特定场景的建议,所以这里是我的几点建议。
- 从您的超参数搜索空间中删除
booster
维度。您可能希望使用默认的提升器’gbtree’。如果您对线性模型的性能感兴趣,您可以尝试线性回归或岭回归,但在XGBoost参数调优期间不要考虑这些。 - 从您的超参数搜索空间中删除
base_score
维度。这在有足够多的提升迭代时应该不会有太大影响(参见XGB参数文档)。 - 目前您的网格中有3200种超参数组合。期望通过查看50个随机组合就能找到一个好的组合可能有点过于乐观。在删除
booster
和base_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]
。也许在这一网格中随机搜索能找到比默认参数更好的结果?
- 另一种策略可能是:为每种组合运行交叉验证
hyperparameter_grid = { 'max_depth': [3, 6, 9], 'min_child_weight': [1, 10, 100] }
将学习率固定在某个常数值(不要太低,例如0.15)。对于每种设置,使用早期停止来确定适当的树的数量。这可以通过xgboost.cv方法的early_stopping_rounds
参数实现。之后,您将知道max_depth
和min_child_weight
的一个好的组合(例如,对于给定问题,基础学习器需要多复杂?),以及对于此组合和固定学习率的一个好的树的数量。然后,微调可以包括在当前的(max_depth, min_child_weight)解决方案附近进行另一次超参数搜索,和/或在增加树的数量的同时降低学习率。