【发布时间】:2021-12-15 14:21:12
【问题描述】:
我正在尝试将 XGBoost 用于包含大约 500,000 个观察值和 10 个特征的特定数据集。我正在尝试使用RandomizedSeachCV 进行一些超参数调整,并且具有最佳参数的模型的性能比具有默认参数的模型差。
具有默认参数的模型:
model = XGBRegressor()
model.fit(X_train,y_train["speed"])
y_predict_speed = model.predict(X_test)
from sklearn.metrics import r2_score
print("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]
## Hyper Parameter Optimization
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]
# Define the grid of hyperparameters to search
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
}
# Set up the random search with 4-fold cross validation
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_score
print("R2 score:", r2_score(y_test["speed"],y_pred, multioutput='variance_weighted'))
R2 score: 0.14258774171629718
正如您在运行随机搜索 3 小时后所看到的那样,准确度实际上有所下降。如果我将线性更改为树,值会上升到 0.65,那么为什么随机搜索不起作用?
我还收到以下警告:
这可能不准确,因为某些参数仅用于 语言绑定,但传递给 XGBoost 核心。或者一些 参数未使用,但通过此验证。请 如果您发现上述情况,请打开问题。
有人对这种超参数调优方法有什么建议吗?
【问题讨论】:
标签: python machine-learning scikit-learn xgboost