ComenzarEmpieza gratis

Ajuste de colsample_bytree

Ahora toca ajustar "colsample_bytree". Ya lo habrás visto si alguna vez trabajaste con RandomForestClassifier o RandomForestRegressor de scikit-learn, donde se llamaba max_features. Tanto en xgboost como en sklearn, este parámetro (aunque con nombre diferente) especifica la fracción de variables de la que se elige en cada división de un árbol. En xgboost, colsample_bytree debe indicarse como un número en coma flotante entre 0 y 1.

Este ejercicio forma parte del curso

Extreme Gradient Boosting con XGBoost

Ver curso

Instrucciones del ejercicio

  • Crea una lista llamada colsample_bytree_vals para guardar los valores 0.1, 0.5, 0.8 y 1.
  • Varía de forma sistemática "colsample_bytree" y realiza validación cruzada, exactamente como hiciste antes con max_depth y eta.

Ejercicio interactivo práctico

Prueba este ejercicio y completa el código de muestra.

# 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"]))
Editar y ejecutar código