Python ML XGBoost vs LightGBM vs Random Forest
Random Forest, XGBoost and LightGBM all build trees, but they build them in different ways. This lesson runs all three on the same data and compares the scores.
One split for all three
The split runs once, before any model trains. Every model then sees the same test rows.
from sklearn.datasets import load_breast_cancer
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from lightgbm import LGBMClassifier
from xgboost import XGBClassifier
data = load_breast_cancer()
X_train, X_test, y_train, y_test = train_test_split(data.data, data.target, test_size=0.25, random_state=42)
print("Train rows:", X_train.shape[0])
print("Test rows:", X_test.shape[0])Random Forest first
Random Forest builds trees that vote. Its score is the first row of the table.
forest = RandomForestClassifier(n_estimators=100, random_state=42)
forest.fit(X_train, y_train)
print("Random Forest accuracy:", round(forest.score(X_test, y_test), 4))Count the mistakes
An error count shows the gap in whole rows. XGBoost misses five rows, and LightGBM misses seven.
for name, model in {"XGBoost": XGBClassifier(n_estimators=100, random_state=42), "LightGBM": LGBMClassifier(n_estimators=100, random_state=42, verbose=-1)}.items():
model.fit(X_train, y_train)
errors = int((model.predict(X_test) != y_test).sum())
print(name, "errors:", errors, "of", len(y_test))Example
from sklearn.datasets import load_breast_cancer
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import f1_score
from sklearn.model_selection import train_test_split
from lightgbm import LGBMClassifier
from xgboost import XGBClassifier
data = load_breast_cancer()
X_train, X_test, y_train, y_test = train_test_split(
data.data, data.target, test_size=0.25, random_state=42)
models = {
"Random Forest": RandomForestClassifier(n_estimators=100, random_state=42),
"XGBoost": XGBClassifier(n_estimators=100, random_state=42),
"LightGBM": LGBMClassifier(n_estimators=100, random_state=42, verbose=-1),
}
for name, model in models.items():
model.fit(X_train, y_train)
pred = model.predict(X_test)
print(name, "accuracy:", round(model.score(X_test, y_test), 4),
"F1:", round(f1_score(y_test, pred), 4))Key points
- Fair comparison: the same rows, the same split, and the same seed for every model
- Use random_state=42 on the split so every model sees the same test rows
- Accuracy: the fraction of test rows the model labels right
- F1: one number that balances precision and recall
- Use one table to hold every score, so the winner is easy to see
In this example, the split uses test_size=0.25, so the test set holds 143 rows. The models dict holds one of each model, all with 100 trees and random_state=42. The loop fits each model on the same training rows, predicts the same test rows, and prints accuracy and F1. Random Forest and XGBoost tie on accuracy at 0.965, with XGBoost a hair behind on F1. LightGBM trails both at 0.951. Pick Random Forest for a quick strong baseline, XGBoost when you want the best tuned score, and LightGBM when the table is big and speed matters. In short, a fair comparison is one split shared by every model, and one table holding every score.
Note
Random Forest and XGBoost tie on accuracy here, but their F1 scores differ by a hair.
Try it Yourself
Edit the code, then press Run.
Running is simulated · Python Playground
Exercise
Why do all three models share one split in this lesson?