Random Forests

Random Forests Cheat Sheet

1. Random Forests Cheat Sheet

Classification
Regression
Supervised Learning
Ensemble

High-Level Overview:

Random Forests are ensemble methods that combine multiple Decision Trees to produce more robust predictions. By averaging the predictions of many diverse trees, a Random Forest reduces variance, improves generalization, and often outperforms a single decision tree.

They can handle both classification (e.g., predicting if an email is spam) and regression (e.g., estimating housing prices) tasks. They work particularly well on tabular data, meaning data organized in rows and columns, where each row is a sample and each column is a feature. For example, a spreadsheet of homes, where each row represents a home and columns represent attributes like square footage, number of bedrooms, and zip code.

Key Concepts:

  • Ensemble of Trees: A Random Forest grows multiple decision trees on different random subsets of data and features.
  • Bagging (Bootstrap Aggregation): Each tree is trained on a random sample (with replacement) of the training data.
  • Feature Subsetting: At each split, only a random subset of features is considered, ensuring diverse trees.
  • Aggregation of Predictions: For classification, the class chosen by the majority of trees is the final output; for regression, the average prediction of all trees is used.

How Random Forest Works:

  1. Bootstrap Sampling: Draw multiple “bootstrapped” datasets from the original training data (e.g., if you have a CSV file of house listings, each “bootstrapped” dataset is like sampling rows from that CSV with replacement).
  2. Grow Each Tree: For each tree:
    • At each split, consider only a random subset of features. If your dataset includes {location, size, number_of_rooms, age_of_house, neighborhood_crime_rate}, each tree might consider a different subset at each node.
    • Grow the tree fully, often without pruning, until stopping criteria are met.
  3. Combine Results: Once all trees are trained:
    • For classification (e.g., predicting if a customer will buy a product), use majority vote of the trees.
    • For regression (e.g., predicting the price of a used car), average the predicted values of all trees.

Why This Reduces Overfitting:

A single decision tree can latch onto quirks of the training data, overfitting it. By averaging many trees, each trained on slightly different data and feature subsets, random forests smooth out these idiosyncrasies, resulting in more stable and generalizable predictions.

Hyperparameters to Consider:

  • Number of Trees (n_estimators): More trees can improve performance but also add computational cost.
  • Max Features: Controls how many features are considered at each split, balancing tree diversity and individual tree strength.
  • Max Depth or Min Samples Leaf: Limits how complex each tree can become, helping prevent overfitting.

What Kind of Datasets Are Random Forests Good For?

Random Forests excel on:

  • Tabular Data: Rows represent samples (e.g., houses), and columns represent features (e.g., size, location, price). This could be real estate listings with features like square footage, number_of_rooms, zip_code, etc.
  • Real-World Structured Problems:
    • Real Estate: Predicting home prices from a dataset of home sales where each record lists features like square footage, neighborhood school quality, lot size, and year built.
    • Medical Diagnostics: Predicting whether a patient has a certain condition using lab test results, patient age, BMI, cholesterol level, and other clinical measurements.
    • E-Commerce: Classifying whether a user will purchase a product based on browsing history, demographics, item price, and time spent on product pages.
Basically, whenever you have a collection of numeric and/or categorical features describing entities (houses, patients, users, products) and you want to predict an outcome (price, diagnosis, buy/not-buy), a random forest is a strong candidate.

Detailed Example (Classification):

Consider predicting whether a restaurant customer will leave a tip. Your dataset might include features like:

  • Order_Total (numeric)
  • Group_Size (numeric)
  • Weather_Rainy (binary: 0 or 1)
  • TimeOfDay_Morning (binary: 0 or 1, could also have Noon, Evening as additional features)
The random forest:
  • Creates multiple bootstrap samples of your restaurant transaction records, each slightly different.
  • Grows several decision trees, each time considering only subsets of these features at each split.
  • Some trees might find that large groups spending a lot in the morning tend to tip, others might rely more on weather conditions. Combining all their votes yields a more reliable prediction.

Step-by-Step Summary:

  1. Train Multiple Trees: Use bootstrap samples and random subsets of features to grow many diverse trees.
  2. Aggregate Predictions: Combine the predictions from all trees (majority vote for classification or average for regression).
  3. Result: A model that is more robust and often more accurate than a single decision tree, suitable for a wide range of tabular, real-world datasets.

ML Engineer Perspective:

  • Random Forests are a strong baseline: start here before trying more complex methods.
  • They handle noisy data, missing values, and varied feature types gracefully.
  • Feature importance metrics can guide you in understanding which features matter most for your predictions.
  • They can be computationally more expensive than a single tree, but usually not prohibitively so for moderately sized datasets.

Code Example (Python with scikit-learn):


import numpy as np
from sklearn.ensemble import RandomForestClassifier

# Example: Predicting if a customer will tip based on:
# [Order_Total, Group_Size, Weather_Rainy(0/1), TimeOfDay_Morning(0/1)]
X = np.array([
    [20.5,  2, 0, 1],  # Tipped
    [10.0,  1, 1, 0],  # No Tip
    [35.0,  4, 0, 0],  # Tipped
    [50.0,  3, 1, 1],  # Tipped
    [15.0,  2, 1, 0],  # No Tip
    [30.0,  2, 0, 1],  # Tipped
])

y = np.array(['Yes', 'No', 'Yes', 'Yes', 'No', 'Yes'])

model = RandomForestClassifier(n_estimators=100, random_state=42)
model.fit(X, y)

# Predict if a customer will tip:
# Order_Total=25, Group_Size=2, Weather_Rainy=0, TimeOfDay_Morning=0
new_customer = np.array([[25, 2, 0, 0]])
prediction = model.predict(new_customer)
print("Predicted Tip?:", prediction[0])

# Inspect feature importances
print("Feature Importances:", model.feature_importances_)
                

Here we use a synthetic dataset for tip prediction. The random forest’s combination of trees yields a more stable prediction than any single tree alone.

Key Takeaways:

  • Random Forest = Many Decision Trees combined for improved stability and accuracy.
  • Works well on tabular data, which is common in business, healthcare, real estate, and many other fields.
  • Less prone to overfitting than a single decision tree, and can handle a variety of feature types and conditions.
  • Feature importance provides insights into the data’s underlying structure.
Previous
Previous

Support Vector Machines

Next
Next

Decision Trees