This lesson on Decision Trees and Random Forests is hands-on and example-driven. You will be able to construct and evaluate a Random Forest model by understanding the roles of bootstrapping and variable subsetting. You will also learn how to use Out-of-Bag error estimation to determine the model's accuracy and optimize its hyperparameters.
What You'll Be Able To Do
- Explain why Random Forests are generally more accurate than single Decision Trees.
- Describe the process of creating a bootstrapped dataset from original data.
- Apply the concept of variable subsetting when building individual trees in a forest.
- Define "bagging" in the context of ensemble modeling.
- Calculate the prediction for a new sample using the voting mechanism of a Random Forest.
- Evaluate the accuracy of a Random Forest using the Out-of-Bag error rate.
Topics Covered in Decision Trees and Random Forests
- Decision Tree Limitations (0:00 - 0:35) — Decision trees are easy to use but are inaccurate for predictive learning on new samples.
- Random Forest Advantage (0:35 - 1:05) — Random Forests combine the simplicity of trees with flexibility, resulting in vastly improved accuracy.
- Step 1: Bootstrapping Data (1:05 - 2:20) — A bootstrapped dataset is created by randomly selecting samples from the original data with replacement.
- Step 2: Building Trees (2:20 - 4:20) — Build a decision tree using the bootstrapped data, but only consider a random subset of variables at each split.
- Forest Variety (4:20 - 5:00) — Repeating the process creates a wide variety of trees, which makes the Random Forest effective.
- Using the Forest (Voting) (5:00 - 6:20) — To classify a new sample, run it through all trees and use the majority vote for the final prediction.
- Terminology: Bagging (6:20 - 6:40) — The process of bootstrapping data and aggregating results via voting is called bagging.
- Out-of-Bag Dataset (6:40 - 7:45) — The Out-of-Bag (OOB) dataset consists of samples not used to build a specific tree, typically about one-third of the data.
- Estimating OOB Error (7:45 - 9:20) — OOB samples are used to measure accuracy, and the proportion incorrectly classified is the OOB error.
- Optimizing Variables (9:20 - 10:20) — Optimize the forest by comparing OOB error rates across different numbers of variables considered per step.
SQL Cheat Sheet
-
Decision Tree— Simple model that struggles with classifying new samples accuratelySELECT * FROM training_data WHERE feature_A > 5; -
Random Forest— Ensemble method combining many trees for vastly improved accuracySELECT prediction FROM forest_model WHERE patient_id = 101; -
Bootstrapping— Creating same-size dataset by sampling original data with replacementSELECT sample_data FROM original_data ORDER BY RANDOM() LIMIT 4; -
Variable Subsetting— Randomly selecting a few features to consider at each node splitSELECT feature_1, feature_2 FROM available_features LIMIT 2; -
Bagging— Bootstrapping data and aggregating tree predictions via voting -
Out-of-Bag (OOB) Dataset— Data samples not included in a specific bootstrapped dataset (approx 1/3)SELECT COUNT(*) FROM oob_samples WHERE prediction <> actual;
Comparison Table
| Characteristic | Decision Tree | Random Forest |
|---|---|---|
| Core Structure | Single tree model | Ensemble of many trees |
| Data Used | Entire training dataset | Bootstrapped subsets of data |
| Feature Selection | Considers all variables | Random subset of variables |
| Accuracy on New Data | Inaccurate (not flexible) | Vastly improved (flexible) |
Common Pitfalls
- Mistake: Assuming a single Decision Tree is sufficient for predictive learning. Avoid: Use Random Forests or other ensemble methods for better generalization and accuracy.
- Mistake: Forgetting to sample with replacement when creating bootstrapped datasets. Avoid: Ensure samples can be picked more than once to maintain the original dataset size.
- Mistake: Using the same set of variables for splitting every node in a tree. Avoid: Randomly select a subset of variables at every single node split.
- Mistake: Using data that built a tree to estimate its accuracy (overfitting). Avoid: Use the Out-of-Bag (OOB) samples to estimate accuracy reliably.
FAQs
- Why are Decision Trees "not that awesome" in practice? They work great on the data used to create them, but they lack flexibility and are inaccurate when classifying new, unseen samples.
- What is the key difference between bootstrapping and standard sampling? Bootstrapping involves sampling with replacement, meaning the same sample can be selected multiple times to create the new dataset.
- What is "bagging"? Bagging is the combination of bootstrapping the data and then aggregating the results (like voting) from the resulting models to make a final decision.
- How many variables should I consider at each split? A common starting point is the square root of the total number of variables; then test settings above and below this value.
- What proportion of data usually ends up in the Out-of-Bag dataset? Typically, about one-third (1/3) of the original data is not included in any specific bootstrapped dataset.