開始使用免費開始

使用 XGBoost 進行隨機搜尋

GridSearchCV 往往非常耗時,因此在實務上,你可能會改用 RandomizedSearchCV,就像你在本練習要做的事。好消息是,你只需要在 GridSearchCV 的程式碼上做一點修改就能改用 RandomizedSearchCV。關鍵差異在於,你需要指定 param_distributions 參數,而不是 param_grid 參數。

本練習屬於課程

使用 XGBoost 的極端梯度提升

檢視課程

練習說明

  • 建立名為 gbm_param_grid 的參數網格,內容包含:'n_estimators' 的單一值清單(25),以及 'max_depth' 的值清單,範圍介於 211——請使用 range(2, 12)
  • 建立名為 randomized_mseRandomizedSearchCV 物件,並傳入:將參數網格指定到 param_distributions、將 XGBRegressor 指定到 estimator、將 "neg_mean_squared_error" 指定到 scoring5 指定到 n_iter,以及 4 指定到 cv。另外設定 verbose=1,以便更清楚地理解輸出。
  • RandomizedSearchCV 物件擬合到 Xy

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# 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_)))
編輯並執行程式碼