使用 XGBoost 進行隨機搜尋
GridSearchCV 往往非常耗時,因此在實務上,你可能會改用 RandomizedSearchCV,就像你在本練習要做的事。好消息是,你只需要在 GridSearchCV 的程式碼上做一點修改就能改用 RandomizedSearchCV。關鍵差異在於,你需要指定 param_distributions 參數,而不是 param_grid 參數。
本練習屬於課程
使用 XGBoost 的極端梯度提升
練習說明
- 建立名為
gbm_param_grid的參數網格,內容包含:'n_estimators'的單一值清單(25),以及'max_depth'的值清單,範圍介於2到11——請使用range(2, 12)。 - 建立名為
randomized_mse的RandomizedSearchCV物件,並傳入:將參數網格指定到param_distributions、將XGBRegressor指定到estimator、將"neg_mean_squared_error"指定到scoring、5指定到n_iter,以及4指定到cv。另外設定verbose=1,以便更清楚地理解輸出。 - 將
RandomizedSearchCV物件擬合到X與y。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Create the parameter grid: gbm_param_grid
gbm_param_grid = {
'____': [____],
'____': ____(____, ____)
}
# Instantiate the regressor: gbm
gbm = xgb.XGBRegressor(n_estimators=10)
# Perform random search: grid_mse
randomized_mse = ____
# Fit randomized_mse to the data
____
# Print the best parameters and lowest RMSE
print("Best parameters found: ", randomized_mse.best_params_)
print("Lowest RMSE found: ", np.sqrt(np.abs(randomized_mse.best_score_)))