Skip to content
Tutorials Center
My learning

Topics

Lesson 9 of 40 ยท Gradient Boosting Basics

Regularization and Subsampling

Learn the split gain formula, how lambda, alpha and gamma prune trees, and how row and column subsampling add randomness that fights overfitting in XGBoost and LightGBM.

A boosted model can memorize its training data if you let it. Boosting libraries defend against that in two ways: penalties that make a split or a leaf value cost something, and randomness that makes every tree see a different slice of the data. This lesson explains both and shows their effect with small experiments. Chapter 5 returns to them as tuning knobs.

The gain of a split

When a tree considers cutting a node into a left and right child, it scores the split with a gain. Using the gradient sum G and Hessian sum H of each group:

TEXT
gain = 1/2 * [ G_L^2/(H_L+lambda) + G_R^2/(H_R+lambda) - (G_L+G_R)^2/(H_L+H_R+lambda) ] - gamma

Read it as "how much better is the loss after splitting than before", minus a flat fee gamma for adding a leaf. If the best gain is negative, the split is rejected and growth stops there. So the parameters have clear jobs:

Parameter Library names Effect
lambda reg_lambda L2 penalty: shrinks leaf values and lowers gain of lopsided splits
alpha reg_alpha L1 penalty: pushes small leaf values to exactly zero
gamma gamma (XGBoost), min_split_gain (LightGBM) Minimum gain a split must achieve
Python Computing split gain by hand

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The best cut is the one between the two clusters (x <= 3; x <= 5 makes the same partition and ties). Raising lambda shrinks every gain, and a large gamma pushes even the best one below zero, which means "do not split". You can see why these parameters act as pruning: they set how much improvement a split has to earn.

Penalties in a real model

On a dataset with some noise, stronger penalties produce a smoother model with a smaller gap between training and test scores. The next example sweeps reg_lambda while keeping everything else fixed. Look at the training minus test gap, not just the test number.

Python Sweeping reg_lambda

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Training error rises as the penalty grows (in one run it went from about 2 at reg_lambda=0 to about 26 at 100), and the train/test gap narrows. But look at the test column: here it does not improve, because this noisy 600-row problem is already dominated by label noise, and a large penalty simply underfits. Regularization is a dial, not a cure. Whether it helps depends on the data, so decide with validation scores, not with the gap alone.

Subsampling: randomness as regularization

The second family borrows an idea from random forests. Each tree is built from a random subset, so no single quirk in the data dominates.

  • Row subsampling: subsample (a fraction of rows per tree). LightGBM calls its version bagging and needs both subsample (or bagging_fraction) and subsample_freq greater than 0 to turn it on.
  • Column subsampling: colsample_bytree (a fraction of features per tree). XGBoost also has colsample_bylevel and colsample_bynode, and LightGBM has feature_fraction_bynode.

Subsampling trades a little bias for a lot less variance, and it makes training faster because trees see less data. Values around 0.6 to 0.9 are common. Going very low hurts unless you have many rows.

Python Row and column subsampling

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The last line is a classic trap: in LightGBM subsample is silently ignored while subsample_freq stays at 0, so the result equals the unsampled model. In this small, easy dataset the differences are tiny, so do not read them as a ranking. The method is what to keep: compare options with cross-validation on one metric.

flowchart TB
  D["All rows and features"] --> S1["Tree 1: random 70% rows, 50% columns"]
  D --> S2["Tree 2: another random subset"]
  D --> S3["Tree 3: another random subset"]
  S1 --> M["Sum of shrunken trees"]
  S2 --> M
  S3 --> M

Every tree is trained on its own random sample, but the trees are still built one after another, each correcting the current ensemble.

Other brakes you will meet

  • max_delta_step (XGBoost) caps each leaf update, which stabilizes very imbalanced logistic regression.
  • min_child_weight / min_child_samples stop splits that would create tiny leaves.
  • early stopping is regularization over the number of trees, covered in Learning Rate and the Number of Trees.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What happens if the best candidate split has gain below zero after subtracting gamma?

    Gamma is a minimum required improvement. A split that does not clear it is pruned.
  2. 2. Which regularizer can push small leaf values to exactly zero?

    L1 regularization soft-thresholds the gradient sum, so leaves with weak signal end up at zero.
  3. 3. In LightGBM, why might subsample=0.7 have no effect?

    Bagging is only active when subsample_freq is at least 1 (every k iterations).
  4. 4. What is the main reason subsampling reduces overfitting?

    Trees trained on different random subsets make less correlated errors, lowering the variance of the sum.

Summary

  • A split's gain compares loss before and after, minus the fee gamma; non-positive gain stops growth.
  • reg_lambda shrinks leaf values, reg_alpha zeroes weak ones, gamma demands a minimum improvement.
  • Row and column subsampling add randomness that lowers variance and speeds training.
  • In LightGBM, bagging needs subsample_freq >= 1 as well.
  • Judge regularization by validation score first, and use the train-versus-validation gap as a diagnostic, with cross-validation.

Next: Histogram Splits and Tree Growth Strategies