Decision trees, random forests, and boosting
scikit-learn 1.9.1
Choose a tree-based baseline and understand how ensemble methods combine estimators.
On this page
Control tree complexity
Decision trees learn splits by feature and threshold. A deep tree can overfit. Limit parameters such as max_depth or min_samples_leaf, and evaluate on data outside training.
Compare averaging and sequential improvement
Random forests average many randomized trees. Gradient boosting adds trees sequentially to improve the current model for a loss function. Compare both to a simple baseline using the same validation plan.
from sklearn.datasets import load_iris
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
X, y = load_iris(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, stratify=y, random_state=42)
model = RandomForestClassifier(n_estimators=30, max_depth=4, random_state=42)
model.fit(X_train, y_train)
print(len(model.estimators_))
print(model.score(X_test, y_test) > 0.8)
Example output
30 True