# 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. Free notes, key concepts,...

Canonical: https://www.internhack.in/roadmaps/tracks/machine-learning/gradient-boosting
Last updated: 2026-11-28

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](https://www.internhack.in/roadmaps/tracks/machine-learning/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

- [XGBoost documentation](https://xgboost.readthedocs.io/en/stable/) (XGBoost)
- [LightGBM documentation](https://lightgbm.readthedocs.io/en/stable/) (LightGBM)
- [scikit-learn: Gradient boosting](https://scikit-learn.org/stable/modules/ensemble.html) (scikit-learn)

Previous: [Bagging and Random Forests](https://www.internhack.in/roadmaps/tracks/machine-learning/random-forests) · Next: [Support Vector Machines and Kernels](https://www.internhack.in/roadmaps/tracks/machine-learning/support-vector-machines)

## More in Classical Supervised Learning

- [Framing ML Problems and the scikit-learn API](https://www.internhack.in/roadmaps/tracks/machine-learning/ml-problem-framing)
- [Linear Regression and Regularization](https://www.internhack.in/roadmaps/tracks/machine-learning/linear-regression)
- [Logistic Regression and Linear Classifiers](https://www.internhack.in/roadmaps/tracks/machine-learning/logistic-regression)
- [k-Nearest Neighbours and Naive Bayes](https://www.internhack.in/roadmaps/tracks/machine-learning/knn-and-naive-bayes)
- [Decision Trees](https://www.internhack.in/roadmaps/tracks/machine-learning/decision-trees)
- [Bagging and Random Forests](https://www.internhack.in/roadmaps/tracks/machine-learning/random-forests)
- [Gradient Boosting with XGBoost and LightGBM](https://www.internhack.in/roadmaps/tracks/machine-learning/gradient-boosting)
- [Support Vector Machines and Kernels](https://www.internhack.in/roadmaps/tracks/machine-learning/support-vector-machines)

## Related

- [Roadmaps](https://www.internhack.in/roadmaps)
- [Machine Learning Engineer Roadmap](https://www.internhack.in/roadmaps/tracks/machine-learning)
