Sub topic 11
A Stronger Model: Histogram Gradient Boosting
This chapter assumes you have a train/test split (X_train, y_train, X_test, y_test), a mean baseline, and a Ridge pipeline scored with 5-fold cross-validation. You should also know what MAE is. Here you add a second model and compare it to Ridge on equal terms.
Why Ridge is not enough
Ridge fits one straight-line relationship per feature. House prices rarely behave that way. Location effects are a good example: latitude and longitude interact, and a price can jump sharply across a county line. A linear model can only approximate this with a flat tilt.
Gradient boosting builds many small decision trees one after another. Each new tree is fitted to the errors the current ensemble still makes, so the ensemble corrects itself step by step. Trees split the data on thresholds, so they capture non-linear effects and interactions without any extra work from you.
What "histogram" means
A standard tree tries every distinct value of a feature as a possible split point. That is slow on large data. HistGradientBoostingRegressor first groups each feature into at most 255 bins, then searches for splits between bins. This makes training much faster on tens of thousands of rows or more, and the model accepts missing values directly.
Trees compare a feature to a threshold, so the scale of a feature does not change which splits are possible. You do not need StandardScaler here.
Scoring it against Ridge
Use the same data, the same folds setting (cv=5), and the same metric. Otherwise the two numbers are not comparable.
from sklearn.ensemble import HistGradientBoostingRegressor
from sklearn.model_selection import cross_val_score
hgb = HistGradientBoostingRegressor(random_state=42)
scores = cross_val_score(
hgb, X_train, y_train, cv=5, scoring="neg_mean_absolute_error"
)
print("HistGradientBoosting CV MAE:", -scores.mean())The scoring name starts with neg_ because scikit-learn always treats a higher score as better. It negates error metrics so that "maximize the score" works for every metric. Negate the result again to read it as an ordinary MAE.
On this dataset the boosted model typically reaches a clearly lower MAE than Ridge. Remember the target is in units of $100,000, so an MAE of 0.3 means an average miss of about $30,000. Your exact figures can differ slightly between scikit-learn versions.
Common errors
- Comparing different setups. Changing
cv, the metric, or the data between models makes the comparison meaningless. - Scaling out of habit. Adding a scaler is harmless for trees but adds nothing. Keep the model simple.
- Using the test set to choose. The cross-validation scores on the training data are for choosing a model. The test set stays untouched until the final evaluation.
- Forgetting the seed. Binning and any early stopping can involve randomness. Set
random_stateso reruns match.
Exercise
You compare Ridge and HistGradientBoostingRegressor and get CV MAEs of 0.53 and 0.32. A teammate suggests adding StandardScaler to the boosting model to "make the comparison fair". Is that needed, and why?
Show answer
No. Tree splits depend on the ordering of a feature's values, not their scale, so scaling does not change the model. The comparison is already fair because both models use the same training data, the same 5 folds, and the same MAE metric.
Exercise
Why does crossvalscore report negative values when scoring="negmeanabsolute_error", and what do you do to read the MAE?
Show answer
scikit-learn treats higher scores as better, so it negates error metrics. Multiply the mean score by -1 to get the MAE.