Random feature subsets
Quick answer
A random forest combines decision trees and introduces randomness in the features considered at splits. Feature subsampling can reduce similarity among trees, especially when a strong feature would otherwise dominate many splits. Examples run locally with Python 3.13.5, scikit-learn 1.8.0, NumPy 2.3.5 and SciPy 1.17.0 when imported. ByteMind library execution is unverified.
Count the chance of seeing a feature
from fractions import Fractionfrom math import combprint(Fraction(comb(7,1),comb(8,2)))Expected output:
1/4A uniformly sampled size-two subset contains a specified feature in one quarter of draws.
How it works
Bagging varies the training rows; random forests usually vary both rows and split candidates. Suppose a node has eight features and considers two randomly chosen candidates. A useful feature may be absent at that node, allowing a different signal to shape the tree. Each node has its own candidate selection; this differs from choosing a fixed feature subset for an entire model. In scikit-learn 1.8, RandomForestClassifier defaults to max_features="sqrt" and RandomForestRegressor defaults to 1.0. Explicitly set the parameter in comparative experiments. An integer gives a nominal candidate count, a fraction scales with the number of features, and sqrt/log2 give rules based on feature count. The tree implementation may inspect more candidates when needed to find a valid split. Smaller subsets can decorrelate trees but also weaken individual trees. Random forests average regression predictions and class probability vectors for classification. Increasing n_estimators can stabilize the average at a computational cost; max_depth and min_samples_leaf still control individual complexity. The example uses all numeric synthetic data and one CPU thread. Tune on validation or training-fold CV, not on final test data.
Inspect forest aggregation
from sklearn.ensemble import RandomForestClassifier, RandomForestRegressorimport numpy as npX=[[0,0],[1,1],[2,0],[3,1]]; y=[0,0,1,1]m=RandomForestClassifier(n_estimators=9,max_features=1,random_state=7,n_jobs=1).fit(X,y)p=np.mean([t.predict_proba(X) for t in m.estimators_],axis=0)print(bool(np.allclose(p,m.predict_proba(X))))r=RandomForestRegressor(n_estimators=9,max_features=1,random_state=7,n_jobs=1).fit(X,[0,2,4,6])print(bool(np.allclose(r.predict(X),np.mean([t.predict(X) for t in r.estimators_],axis=0))))Expected output:
TrueTrueBoth prediction aggregations match the corresponding forest methods on this fixture.
Common mistakes
Do not assume max_features specifies a permanent column mask, or that a forest cannot overfit. Do not compare a classifier with a regressor using unrelated metrics.
Practice and next steps
For p=10 and k=3, compute the inclusion probability of one feature; then compare max_features=1 and max_features=all on validation data. Complete the knowledge check and coding exercise. Next: Out-of-bag evaluation.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Random feature subsets — knowledge check
Apply random feature subsets to new examples.
Where does a random forest normally resample candidate features?
For p=8 and k=2, inclusion probability for a fixed feature?
Why use smaller feature subsets?
How does a forest regressor combine predictions?
What does n_estimators count?