Ottimizzare colsample_bytree
Ora è il momento di ottimizzare "colsample_bytree". Probabilmente l’hai già incontrato se hai usato RandomForestClassifier o RandomForestRegressor di scikit-learn, dove si chiamava max_features. Sia in xgboost che in sklearn, questo parametro (anche se con nomi diversi) indica semplicemente la frazione di feature tra cui scegliere a ogni split in un dato albero. In xgboost, colsample_bytree deve essere un valore float compreso tra 0 e 1.
Questo esercizio fa parte del corso
Extreme Gradient Boosting con XGBoost
Istruzioni dell'esercizio
- Crea una lista chiamata
colsample_bytree_valsper memorizzare i valori0.1,0.5,0.8e1. - Varia in modo sistematico
"colsample_bytree"ed esegui la cross-validation, esattamente come hai fatto in precedenza conmax_depthedeta.
Esercizio pratico interattivo
Prova a risolvere questo esercizio completando il codice di esempio.
# 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"]))