Gradient Boosting with XGBoost and LightGBM

Sequentially adding small trees that fix previous errors, the go-to method for winning tabular problems in industry and on Kaggle.

Intermediate · about 10 hours · difficulty 3/5

Learn first: Bagging and Random Forests

Gradient boosting builds trees one after another, each fitted to the errors (gradients of the loss) of the ensemble so far. With sensible settings it is the strongest general-purpose model for structured data.

Libraries

  • XGBoost: mature, fast, widely deployed.
  • LightGBM: very fast on large data thanks to histogram and leaf-wise growth.
  • CatBoost: excellent native handling of categorical features.
  • scikit-learn HistGradientBoosting: good built-in option with no extra dependency.

Important hyperparameters

  • learning_rate and n_estimators trade off together: lower rate, more trees.
  • Tree size: max_depth or num_leaves, and min_child_weight / min_data_in_leaf.
  • Randomness: subsample and colsample_bytree reduce overfitting.
  • Regularization: lambda (L2) and alpha (L1) on leaf weights.
  • Early stopping on a validation set picks the number of trees automatically; always use it.

Pitfalls

Early stopping on the test set (leakage), tuning twenty parameters before trying reasonable defaults, and ignoring native categorical and missing-value support that could simplify preprocessing.

Good enough to move on when you can train LightGBM or XGBoost with early stopping, beat your random forest on a Kaggle tabular dataset, and explain what the learning rate does.

Key concepts

  • Boosting intuition and residual fitting
  • XGBoost
  • LightGBM
  • CatBoost
  • Learning rate and number of trees
  • Early stopping
  • Native categorical and missing handling

Mini project

Enter the Kaggle Spaceship Titanic competition with a LightGBM or XGBoost model using early stopping and engineered features, and document your cross-validation versus leaderboard scores.

Check yourself

What happens to the best number of trees when you halve the learning rate, and why?

Free resources