開始使用免費開始

調整 colsample_bytree

現在要來調整 "colsample_bytree"。如果你用過 scikit-learn 的 RandomForestClassifierRandomForestRegressor,其實就看過類似的概念,在那裡這個參數叫做 max_features。在 xgboostsklearn 中,這個參數(雖然名稱不同)都用來指定在每個樹節點進行切分時,可供選擇的特徵比例。在 xgboost 中,colsample_bytree 必須設定為介於 0 到 1 之間的浮點數。

本練習屬於課程

使用 XGBoost 的極端梯度提升

檢視課程

練習說明

  • 建立名為 colsample_bytree_vals 的串列,包含數值 0.10.50.81
  • 有系統地改變 "colsample_bytree" 並進行交叉驗證,方式與先前對 max_deptheta 的做法完全相同。

動手互動練習

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

# Create your housing DMatrix
housing_dmatrix = xgb.DMatrix(data=X,label=y)

# Create the parameter dictionary
params={"objective":"reg:squarederror","max_depth":3}

# Create list of hyperparameter values: colsample_bytree_vals
____ = ____
best_rmse = []

# Systematically vary the hyperparameter value 
for curr_val in ____:

    ____ = ____
    
    # Perform cross-validation
    cv_results = xgb.cv(dtrain=housing_dmatrix, params=params, nfold=2,
                 num_boost_round=10, early_stopping_rounds=5,
                 metrics="rmse", as_pandas=True, seed=123)
    
    # Append the final round rmse to best_rmse
    best_rmse.append(cv_results["test-rmse-mean"].tail().values[-1])

# Print the resultant DataFrame
print(pd.DataFrame(list(zip(colsample_bytree_vals, best_rmse)), columns=["colsample_bytree","best_rmse"]))
編輯並執行程式碼