Labs ICT
⭐ Pro Login

Decision Trees & Random Forests

Tree-based models for classification and regression.

Tree-Based Models

Decision Trees are intuitive, interpretable models that split data into branches based on feature values. Think of it like a flowchart β€” at each node, you ask a question and follow the branch that matches. Random Forests take this further by combining many trees into a powerful ensemble.

How Decision Trees Work


  Should I play tennis today?

                    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                    β”‚  Outlook? β”‚
                    β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”˜
            β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
            β–Ό            β–Ό            β–Ό
        β”Œβ”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”
        β”‚Sunny β”‚    β”‚Overc.β”‚    β”‚ Rain β”‚
        β””β”€β”€β”¬β”€β”€β”€β”˜    β””β”€β”€β”¬β”€β”€β”€β”˜    β””β”€β”€β”¬β”€β”€β”€β”˜
           β”‚           β”‚           β”‚
      β”Œβ”€β”€β”€β”€β”΄β”€β”€β”€β”€β”      β”‚      β”Œβ”€β”€β”€β”€β”΄β”€β”€β”€β”€β”
      β”‚Humidity?β”‚      β”‚      β”‚  Wind?  β”‚
      β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”˜      β”‚      β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”˜
     β”Œβ”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”    Yes    β”Œβ”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”
     β–Ό           β–Ό           β–Ό           β–Ό
  β”Œβ”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”   β”Œβ”€β”€β”€β”€β”€β”€β”   β”Œβ”€β”€β”€β”€β”€β”€β”
  β”‚High  β”‚  β”‚Normalβ”‚   β”‚  Yes β”‚   β”‚  No  β”‚
  β”‚  No  β”‚  β”‚ Yes  β”‚   β””β”€β”€β”€β”€β”€β”€β”˜   β””β”€β”€β”€β”€β”€β”€β”˜
  β””β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”˜

Each internal node represents a test on a feature, each branch represents the outcome, and each leaf represents a class label. The tree learns which splits best separate the classes by maximizing information gain or minimizing Gini impurity.

The Problem with Single Trees

A single decision tree tends to overfit β€” it memorizes the training data, including its noise. It creates overly complex trees that don't generalize well. That's where Random Forests come in.

Random Forests: Strength in Numbers

A Random Forest builds hundreds or thousands of decision trees on random subsets of the data and features. Each tree votes on the prediction, and the majority wins. This bagging approach reduces overfitting and improves generalization.


  Random Forest:

  Tree 1:  🌳 β†’ Predicts: Spam
  Tree 2:  🌳 β†’ Predicts: Not Spam
  Tree 3:  🌳 β†’ Predicts: Spam
  Tree 4:  🌳 β†’ Predicts: Spam
  Tree 5:  🌳 β†’ Predicts: Not Spam
  ... (hundreds more)
  Tree N:  🌳 β†’ Predicts: Spam

  Majority Vote β†’ Final: SPAM βœ“

Feature Importance

Both Decision Trees and Random Forests can tell you which features are most important for predictions. This is valuable for understanding your data and explaining model decisions β€” a big advantage over "black box" models like neural networks.

When to Use Them

Tree-based models are great defaults for tabular (structured) data. They handle mixed feature types (numerical and categorical), require minimal preprocessing, are robust to outliers, and provide feature importance. Random Forests are often the best starting point for any classification or regression task on tabular data.

πŸ§ͺ Quick Quiz

What is a Random Forest?