使用网格搜索调整超参数会导致过度拟合
Tuning the hyperparameter with gridsearch results in overfitting
使用网格搜索调整超参数会导致过度拟合。
train error肯定是低的,但是test error是高的。不能调整超参数来降低测试误差吗?
def custom_wmae(actual_values, predicted_values):
weight = actual_values.values / sum(actual_values)
diff = abs(predicted_values - actual_values.values)
return np.sum(weight * diff)
param_test1 = { 'max_depth':range(3,10,2),
'min_child_weight':range(1,6,2)}
xgb1_test1 = xgboost.XGBRegressor(
learning_rate =0.1,
n_estimators=140,
max_depth=5,
objective ='reg:squarederror',
min_child_weight = 1,
subsample=0.8,
scale_pos_weight=1,
gamma = 0,
seed=27)
grid_search = GridSearchCV(estimator=xgb1_test1,param_grid= param_test1, cv=5,
scoring=make_scorer(custom_wmae, greater_is_better=False),
iid=False,
return_train_score=True)
params_result= grid_search.fit(shuffled_train_X, shuffled_train_y)
- 调整前
train_error:0.386055,test_error:0.674069
-调整后
train_error:0.070645,test_error:0.708254
这完全取决于您训练的数据。如果您用于训练的数据非常少,比方说 500 行和几列,即使这样您也试图拆分成训练和测试数据。 XGBoost 最有可能过度拟合训练数据。
为确保您的模型不会过度拟合,您可以尝试三件事 -
确保您有足够的数据用于 XGBoost 训练。否则,过拟合的倾向将一直存在。
使用您的参数。尝试使用 L1 和 L2 正则化对数据引入正则化。
在官方的XGBoostAPI中,可以在'xgb.train()'函数中通过验证集。因此,您可以在函数的 eval_set 参数中传递您的测试集。
使用网格搜索调整超参数会导致过度拟合。
train error肯定是低的,但是test error是高的。不能调整超参数来降低测试误差吗?
def custom_wmae(actual_values, predicted_values):
weight = actual_values.values / sum(actual_values)
diff = abs(predicted_values - actual_values.values)
return np.sum(weight * diff)
param_test1 = { 'max_depth':range(3,10,2),
'min_child_weight':range(1,6,2)}
xgb1_test1 = xgboost.XGBRegressor(
learning_rate =0.1,
n_estimators=140,
max_depth=5,
objective ='reg:squarederror',
min_child_weight = 1,
subsample=0.8,
scale_pos_weight=1,
gamma = 0,
seed=27)
grid_search = GridSearchCV(estimator=xgb1_test1,param_grid= param_test1, cv=5,
scoring=make_scorer(custom_wmae, greater_is_better=False),
iid=False,
return_train_score=True)
params_result= grid_search.fit(shuffled_train_X, shuffled_train_y)
- 调整前 train_error:0.386055,test_error:0.674069
-调整后 train_error:0.070645,test_error:0.708254
这完全取决于您训练的数据。如果您用于训练的数据非常少,比方说 500 行和几列,即使这样您也试图拆分成训练和测试数据。 XGBoost 最有可能过度拟合训练数据。
为确保您的模型不会过度拟合,您可以尝试三件事 -
确保您有足够的数据用于 XGBoost 训练。否则,过拟合的倾向将一直存在。
使用您的参数。尝试使用 L1 和 L2 正则化对数据引入正则化。
在官方的XGBoostAPI中,可以在'xgb.train()'函数中通过验证集。因此,您可以在函数的 eval_set 参数中传递您的测试集。