Skip to content

    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

    Age > 30?yes → Income > 50k?no → Student?leaves = class

    Flowchart

    Pick best feature splitSplit dataRepeat on childrenStop at max depthForest: bag many trees and vote

    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.

    More Machine Learning topics