Squared-error gradient boosting adds small learners to correct the current training residuals, while a separate validation period decides when the sequence stops.
Gradient boosting residuals and early stopping
Fit errors in sequence
For a clearance-time target, start with the training mean. Compute each training residual as actual minus current prediction, fit a shallow tree to those residuals, then add a fraction of the tree output to the prediction. Each stage depends on the preceding stages; this is the key difference from independently bagged trees. The code uses a declared backlog split and group means to expose the update, while a full implementation searches splits and supports other differentiable losses.
Treat the learning rate and stage count as a pair
A small learning rate makes each correction modest, often requiring more stages. Too many stages can fit noise even when each individual tree is shallow. Tree depth and minimum leaf support also matter. Compare training and validation error at each stage on a fixed development partition. Training-size curves answer a different question: whether more rows improve a fixed model design.
Stop without peeking at the final test
Choose the stage with the lowest development-validation loss, preferably with a tolerance or patience rule defined before looking. A future test remains sealed until the fitted settings are frozen. Repeatedly changing the rate, split policy or feature set after inspecting that final test converts it into development data. The selection lesson states the boundary.
Know what the validation curve cannot prove
A decreasing validation error on one convenient random split does not prove that the model handles a new warehouse. A new site or later period can have different label delay, staffing policy and feature coverage. Use groups and time cuts matching the deployment question. Report the selected stage, validation support and error slices. Tail errors may worsen while average squared error improves.
Compare the actual service budget
Boosted stages execute sequentially at prediction time, unlike independently trained bagged trees. Distillation, fewer stages or shallower trees may be needed under a tight latency budget. An expensive model must beat the operational baseline on the same future rows by enough to justify training, storage, serving and review costs. The release project makes that decision explicit.
Implementation
training_rows = [(8, 3), (12, 4), (17, 5), (24, 7), (29, 9), (35, 10)]
validation_rows = [(14, 4), (27, 8), (33, 9)]
cutoff = 21
learning_rate = 0.35
initial_hours = sum(hours for _, hours in training_rows) / len(training_rows)
training_predictions = [initial_hours] * len(training_rows)
validation_predictions = [initial_hours] * len(validation_rows)
stage_losses = []
for _ in range(12):
residuals = [actual - predicted for (_, actual), predicted
in zip(training_rows, training_predictions)]
left = [residual for (backlog, _), residual in zip(training_rows, residuals)
if backlog < cutoff]
right = [residual for (backlog, _), residual in zip(training_rows, residuals)
if backlog >= cutoff]
left_step = sum(left) / len(left)
right_step = sum(right) / len(right)
training_predictions = [
predicted + learning_rate * (left_step if backlog < cutoff else right_step)
for (backlog, _), predicted in zip(training_rows, training_predictions)
]
validation_predictions = [
predicted + learning_rate * (left_step if backlog < cutoff else right_step)
for (backlog, _), predicted in zip(validation_rows, validation_predictions)
]
loss = sum((actual - predicted) ** 2 for (_, actual), predicted
in zip(validation_rows, validation_predictions)) / len(validation_rows)
stage_losses.append(loss)
selected_stage = min(range(1, len(stage_losses) + 1),
key=lambda stage: stage_losses[stage - 1])
assert stage_losses[0] < sum((actual - initial_hours) ** 2
for _, actual in validation_rows) / len(validation_rows)
assert 1 <= selected_stage <= 12Performance and operating cost
With T stages, N training rows and a fixed two-leaf split, this illustration costs O(TN) time and O(N) working memory. Searching all features and thresholds raises fitting cost substantially. Serving evaluates stages in order and scales approximately with the number of stored trees.
Common Mistakes
- Do not fit stage residuals from validation outcomes.
- Do not use the final test to select the number of stages.
- Do not assume lower squared error also fixes costly underestimation tails.
