Decision Trees & Random Forest
Machine Learning · Intermediate
Definition
A decision tree splits data with if/else questions; a random forest averages many trees for better accuracy.
Trees are interpretable and forests are strong on tabular data.
Diagram
Flowchart
Example: Random forest
from sklearn.ensemble import RandomForestClassifier
X=[[0,0],[1,1],[0,1],[1,0]]; y=[0,1,0,1]
print(RandomForestClassifier(random_state=0).fit(X,y).predict([[1,1]]))Output: [1]
Key points
- Splits use Gini or entropy
- Single trees overfit
- Forests reduce variance
Interview questions
What is bagging?
Training models on random bootstrap samples and averaging them.