Boosting controls: early stopping, subsampling and monotone constraints
Stop at the right round without touching test rows, add randomness to steady the fit, and force an effect's direction when domain knowledge demands it.
What runs in CodeSplash
You will be able to
- Use early stopping with its own validation share inside each fold
- Add row and column subsampling, and say what they do
- Impose monotone constraints and weigh their cost in accuracy
Read first
Let the data pick the rounds
ReadEarly stoppingEarly stoppingStopping training when a score on held-back rows has stopped improving for a set number of rounds. watches a validation score while it adds trees, and stops when that score hasn't improved for Early stopping rounds in a row.
It needs rows to watch. Early-stopping validation share holds some back from each fold's training rows: not the fold's validation rows, and never the test set. Those stay honest.
Worth setting?
PredictPredict first
Penguins, log loss. A plain run uses 25 rounds at a learning rate of . A second has a budget of 300 rounds at a rate of , with early stopping after 5 rounds of no progress. What happens to the second?
Reveal
About the same or better: 0.0537 against 0.0991 for the plain run, on slightly different features and folds. It didn't use its budget: it stopped once the watched score stalled. So set a generous budget, and let the stop decide.
Practice stopping
ExploreFind where New-data MSE bottoms out while Training MSE keeps falling. That's the point early stopping looks for. Now cut Learning rate (boosting) in half and find it again.
DExplorable · Bagging and boosting
How do many weak trees combine into a strong model?
With up to 200 trees available, at how many trees will error on new data be lowest?
Method
Commit your prediction above to see the result
Make your prediction to see the result.
Stirring in randomness
ReadRow subsample and Column subsample per tree give each round a different view of the data. Like bagging, that lowers variance and speeds up fitting. On ozone, with both at 80%, XGBoost has an RMSE of 18.2, and LightGBM with row and column bagging 18.6.
Minimum rows per leaf and L2 penalty work the same way, as brakes on individual trees.
Enforcing a direction
ReadSome effects have a direction you can defend. Higher temperature should not lower predicted ozone, and a lower credit score shouldn't lower predicted risk.
Monotone constraintsMonotone constraintA rule that forces predictions to only rise, or only fall, as one feature grows. forbid any split that breaks the rule. Set Monotone constraints to a list such as Temp: +1, Wind: -1. That also makes the model easier to defend, because it can't give a perverse answer in a region with few rows.
The price of a rule
PredictPredict first
Ozone RMSE with free trees is 18.2. You require more temperature and sun not to lower the prediction, and more wind not to raise it. What happens?
Reveal
It costs a little: RMSE 19.7, on a run that also stops early. On the test rows it's 16.1 against 16.5. A rule like that may be cheap, or even pay for itself on new data. Measure the price before arguing about it.
Check your understanding
CheckpointCheck yourself
Where does early stopping get the rows it watches, in the app?
Show the answer
Answer: c
Watching the fold's validation rows would let that score influence training. A separate share keeps the cross-validated score honest.
Check yourself
What does setting Row subsample to 0.8 do?
Show the answer
Answer: a
It's sampling per round, so every row still contributes across rounds. The idea is called stochastic gradient boosting.
Check yourself
When is a monotone constraint most useful?
Show the answer
Answer: b
Use it for rules you can defend, and not for patterns you found in these same rows. Then compare scores with and without the rule.
Use them
In the appIn XGBoost or LightGBM under Hyperparameters, set Early stopping rounds, Row subsample and Monotone constraints. LightGBM also has Regression loss, which offers a quantile objective: with Quantile at 0.8, the model predicts an upper range for ozone rather than its average.
import pandas as pd
from sklearn.model_selection import train_test_split
from xgboost import XGBRegressor
df = pd.read_csv("airquality.csv").dropna()
X, y = df[["Solar.R", "Wind", "Temp"]], df["Ozone"]
Xtr, Xval, ytr, yval = train_test_split(X, y, test_size=0.2, random_state=1)
for rules in [None, (1, -1, 1)]:
m = XGBRegressor(n_estimators=500, learning_rate=0.1, subsample=0.8, early_stopping_rounds=10,
monotone_constraints=rules, eval_metric="rmse")
m.fit(Xtr, ytr, eval_set=[(Xval, yval)], verbose=False)
print(rules, m.best_iteration, round(m.best_score, 2))library(xgboost)
df <- na.omit(airquality)
X <- as.matrix(df[, c("Solar.R", "Wind", "Temp")])
set.seed(1)
hold <- sample(nrow(X), round(0.2 * nrow(X)))
dtr <- xgb.DMatrix(X[-hold, ], label = df$Ozone[-hold])
dva <- xgb.DMatrix(X[hold, ], label = df$Ozone[hold])
for (rules in list(c(0, 0, 0), c(1, -1, 1))) {
fit <- xgb.train(list(learning_rate = 0.1, subsample = 0.8, monotone_constraints = rules, eval_metric = "rmse"),
dtr, nrounds = 500, evals = list(val = dva), early_stopping_rounds = 10, verbose = 0)
cat(rules, xgb.attr(fit, "best_iteration"), round(as.numeric(xgb.attr(fit, "best_score")), 2), "\n")
}Recap
Recap- Early stopping lets a generous budget stop itself, watching rows held back inside each fold.
- Subsampling rows and columns adds bagging's steadiness to boosting.
- Monotone constraints enforce a defensible direction. Measure the price in score.
Concepts in this lesson