AI engineering glossary · Classical ML algorithms
What is RF (random forest)?
Random forest (also called RF): An ensemble of many decision trees, each trained on a bootstrap sample and allowed to consider only a random subset of features at every split. Predictions are averaged or voted. It is accurate, resists overfitting and needs little tuning, which makes it a popular tabular-data baseline.
Random forest: an example
RandomForestClassifier(n_estimators=300) builds 300 trees on churn data and predicts the class most trees choose.
Random forest in Python
A short example you can run (Python 3.11, scikit-learn). The output below is what it really prints.
from sklearn.datasets import load_breast_cancer
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
X, y = load_breast_cancer(return_X_y=True)
Xtr, Xte, ytr, yte = train_test_split(X, y, random_state=0)
rf = RandomForestClassifier(n_estimators=200, random_state=0).fit(Xtr, ytr)
print("test accuracy:", round(rf.score(Xte, yte), 3))
print("trees:", len(rf.estimators_))Output
test accuracy: 0.972 trees: 200
Related terms
More Classical ML algorithms terms
- RBF kernel
- Regression tree
- Residual
- Ridge regression
- Scree plot
- Shrinkage
- Sigmoid function
- Silhouette score
Where you learn it
Random forest is part of the AI Engineering Program, which teaches it with notes, a hands-on lab and an exam. The first lesson is free.