Skip to content

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.

Advanced ~12 min9 steps

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

01

Let the data pick the rounds

Read

Early 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.

02

Worth setting?

Predict

Predict first

Penguins, log loss. A plain run uses 25 rounds at a learning rate of 0.20.2. A second has a budget of 300 rounds at a rate of 0.30.3, with early stopping after 5 rounds of no progress. What happens to the second?

It overfits with such a large budgetIt does about the same or betterIt underfits because of the high rate
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.

03

Practice stopping

Explore

Find 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?

101
How sure?

Method

25
0.20
2
0.35
80
Simulating…

Commit your prediction above to see the result

Make your prediction first; the figure appears when you commit.

Make your prediction to see the result.

04

Stirring in randomness

Read

Row 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.

05

Enforcing a direction

Read

Some 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.

06

The price of a rule

Predict

Predict 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?

It costs a lot of accuracyIt costs a little accuracyIt always improves accuracy
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.

07

Check your understanding

Checkpoint

Check 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.

08

Use them

In the app

In 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.

Python
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))
R
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")
}
09

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