Machine Learning · easy
Asked in ML Engineer interviews, in the Machine Learning round.
Bagged decision trees with feature subsampling; average/vote to reduce variance.
Many decision trees, each trained on a different random view of the data, whose predictions are averaged.
Two sources of randomness, and each has a job:
Individual deep trees overfit; they have low bias and high variance. Averaging many trees whose errors are only weakly correlated keeps the low bias and cuts the variance. That is the whole idea, and it is why more trees never hurt (only cost time) and why the forest is hard to overfit by adding trees.
from sklearn.ensemble import RandomForestClassifier
rf = RandomForestClassifier(n_estimators=500, max_features="sqrt", oob_score=True, n_jobs=-1).fit(X, y)
rf.oob_score_ # free validation estimate from the left-out bootstrap rows
rf.feature_importances_
Out-of-bag error is a nice detail: each tree left out about a third of the rows, so those rows give a validation estimate without a holdout set. On feature importance, say that impurity-based importance inflates high-cardinality features and permutation importance is more honest.