Topic 2 of 6 · 5 min read
Decision trees, random forests and gradient boosting
Goal: Train a gradient-boosted tree model on a tabular dataset and compare it with a linear baseline on the same split.
A decision tree asks a series of yes/no questions about your columns; random forests and gradient boosting combine hundreds of such trees, and on tabular data they are usually the first thing to try. Tabular data means rows and columns: customers, orders, sensor readings, loan applications.
Check yourself · 1 of 3
What is the main difference between a random forest and gradient boosting?
Why it matters
A 2022 NeurIPS benchmark paper (Grinsztajn and colleagues, see the references) found that tree-based models still beat deep learning on medium-sized tabular datasets, and they train in minutes on a laptop. Trees handle mixed column types, non-linear effects and feature interactions without manual work, and need little scaling. If an assistant suggests a neural network for a 50,000-row spreadsheet, start with trees instead.
How they work
- One tree splits rows by a threshold, for example
age < 30, choosing the split that makes the groups purer. Deep single trees memorise the training data and overfit. - Random forest trains many deep trees on random samples of rows and columns, then averages them. It is hard to break and needs little tuning.
- Gradient boosting trains small trees one after another, each fixing the errors of the previous ones. It usually scores higher than a forest but is more sensitive to settings. XGBoost, LightGBM and CatBoost are fast, widely used implementations; scikit-learn has
HistGradientBoostingClassifier. CatBoost also encodes categorical columns for you.
Settings that matter most for boosting: learning_rate (smaller is safer), n_estimators (use early stopping on a validation set), and max_depth (3 to 8 is typical).
Always keep a baseline
Fit a simple model on the same split and compare. If boosting beats logistic regression by a wide margin, the extra complexity is justified. If the gap is tiny, ship the simple model.
from sklearn.ensemble import HistGradientBoostingClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import roc_auc_score
base = LogisticRegression(max_iter=1000).fit(X_train_scaled, y_train)
gb = HistGradientBoostingClassifier(learning_rate=0.05, early_stopping=True,
random_state=0).fit(X_train, y_train)
print("linear", roc_auc_score(y_test, base.predict_proba(X_test_scaled)[:, 1]))
print("boosted", roc_auc_score(y_test, gb.predict_proba(X_test)[:, 1]))
What goes wrong
- Overfitting a single tree with no depth limit: perfect training score, poor test score.
- Tuning on the test set. Use a validation split or cross-validation, and touch the test set once.
- Extrapolation. Trees predict values inside the range they saw. A price model trained on 2023 prices cannot predict a price higher than any it saw.
- Expecting gains on small or very noisy data, where a regularized linear model may match the boosted one.
Examples
Baseline first
A churn model: logistic regression gives ROC AUC 0.81 on the test split, HistGradientBoostingClassifier gives 0.86. The gap is worth keeping the boosted model. If it had been 0.81 against 0.82, the linear model would be easier to explain, cheaper to serve and nearly as good.
The perfect tree
DecisionTreeClassifier().fit(X_train, y_train) # no max_depth
Training accuracy is 100 percent and test accuracy is far lower, because the tree grew until every training row had its own leaf. Set max_depth or use a forest.
Key points
- Trees split on thresholds, so they handle mixed column types and non-linear effects with little preprocessing.
- A random forest averages many deep trees and is forgiving; gradient boosting builds trees in sequence and usually scores higher but needs tuning.
- Benchmarks such as Grinsztajn et al. (2022) show tree ensembles still lead on medium-sized tabular data.
- Use early stopping on a validation set to choose the number of boosting rounds.
- Always fit a simple baseline on the same split; keep the boosted model only if the gap earns its complexity.
- Trees cannot predict beyond the range of target values seen in training.
Common mistakes
- Choosing a neural network for a spreadsheet-sized dataset before trying a boosted tree.
- Reporting the score from the tuning split as the final result instead of a held-out test set.
- Setting many boosting rounds with no early stopping and a high learning rate.
- Expecting a tree model to extrapolate a trend beyond the training range.
Check yourself · 2 of 3
A single decision tree with no depth limit scores 100 percent on training data and 70 percent on test data. What is the best next step?
Check yourself · 3 of 3
A boosted model must predict house prices up to 2 million, but all training prices are below 1.2 million. What should you expect?
Practice items, not counted towards your mastery.
Start here
- scikit-learn: ensembles (gradient boosting, random forests) (opens in a new tab)DocsFree
Reference for random forests and histogram gradient boosting, with the parameters that matter.
- XGBoost documentation (opens in a new tab)DocsFree
Official docs for the most used boosted-tree library, including parameter tuning notes.
- Why do tree-based models still outperform deep learning on tabular data? (opens in a new tab)PaperFree
The benchmark paper behind the claim that trees lead on medium-sized tabular data.