ML3: Classic models with scikit-learn

Lesson 3 of 5 · reading 15–25 minutes; practice and project time is on the module page

Ensembles: random forests and gradient boosting

🎯You'll be able to

Explain how random forests and gradient boosting combine many trees, and train both.

🏠The everyday picture

Asking one friend for a restaurant tip is hit and miss. Asking twenty friends and going with the most common answer is more reliable. That's a random forest. Gradient boosting is more like a teacher marking drafts: each new draft fixes what the last one got wrong.

📘What it really is

📝A worked example

Input: three candidates trained on January to April, scored on May (reference measurements, 2 November 2026):

Model Settings Training PR-AUC Validation PR-AUC
Random forest 300 trees, min_samples_leaf=5 0.8695 0.5215
Logistic regression scaled, default C — 0.5513
Gradient boosting 200 trees, rate 0.05, 15 leaves, L2 1.0 0.8236 0.5617

Gradient boosting is best on May, so the kit chooses it. Every model scores much higher on training rows than on May; Lesson 4 is about that gap.

🌍Where it's used

📝Case scenario

A team ran a gradient boosting model with a high learning rate and 2,000 trees. It looked good on training data and poor on new data. With a lower rate, fewer leaves and early stopping on validation data, it scored a little lower in training and better on new data.

🖱️Try it

Train a random forest with n_estimators=50 and then 300, both with random_state=0. Compare May's PR-AUC and the time each takes. Success: you can say whether more trees helped enough to be worth the extra time.

⚠️Common mistakes

🛠️For your project

Project 1 trains logistic regression and gradient boosting, records both on May and June, and keeps the one chosen on May.

✅Quick check

How does a random forest reduce the mistakes of single trees?

Show the answer

It averages many different trees, so their mistakes partly cancel out.

What does each new tree do in gradient boosting?

Show the answer

It focuses on the errors the trees before it still make.

← PreviousDecision trees
ProLearners Assist