/W09
实验Lab: Forests and boosting
- Dataset
- California housing (20,640 districts, 1990 US Census)
- Time
- 3 hours
- Chapters
- Ch 12: Ensemble Methods
- Before you start
- slides ch 12 quiz ch 12 notebook (opens in a new tab)
This week you'll be able to
- Explain why bagging lowers variance and boosting lowers bias.
- Train and tune a random forest and a gradient-boosted model, and compare them with a single tree.
- Read feature importances and say what they do and do not show.
Goal
Beat a single tree with bagging and boosting on a regression problem. Tune them properly and read what their importances do and don't show.
Data
The dataset you explored in week 7. Reuse your notes on the capped values.
from sklearn.datasets import fetch_california_housing
from sklearn.model_selection import train_test_split
X, y = fetch_california_housing(return_X_y=True, as_frame=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)Tasks
- Fit a linear regression and a single decision tree. Report test RMSE for both. These are your baselines.
- Fit a random forest. Tune
n_estimatorsandmax_depth(Procedure 12.1). Plot validation RMSE against the number of trees. Where does it level off? - Fit a gradient-boosted model:
HistGradientBoostingRegressor, XGBoost, or LightGBM. Use early stopping on a validation split. - Compare all four models in one table: test RMSE, training time, and prediction time. Use Procedure 12.3 to say which you'd ship.
- Plot feature importance for the forest (Procedure 12.2). Then compute permutation importance on the test set. Where do they disagree, and why?
- Look at the 20 largest errors of your best model. How many are capped districts?
Deliverable
A notebook with the comparison table, both importance plots, and a paragraph on task 6.
Done when
- Every ensemble beats the single tree on test RMSE.
- Early stopping is on a validation split, not the test set.
- The importance comparison says what each method measures.
Stretch
Build a stacking ensemble from the forest, the boosted model, and a ridge regression (Procedure 12.4). Does it beat the best single model by enough to justify it?