Bootstrapping, Bagging, Random Forests & Feature Importance
By the end of this lesson, you should be able to:
Machine learning models can be sensitive to the training data.
Ensemble learning improves predictions by combining multiple models.
The key idea:
Instead of relying on one model, combine many models.
Two concepts drive today’s lesson:
Bootstrapping is resampling with replacement.
Given a dataset with (n) observations:
Example:
A B C D E
Bootstrap sample:
A C C E B
Notice:
Starting with:
A B C D E
We might create:
| Sample | Bootstrap dataset |
|---|---|
| 1 | A C C E B |
| 2 | D D A B E |
| 3 | B A E E C |
| 4 | C C D A A |
Each sample:
This gives us different versions of the training data.
Now train a separate model on each bootstrap sample.
This is Bagging:
Bootstrap Aggregating
The process:
Training Data
↓
Bootstrap Samples
↓
Multiple Models
↓
Aggregate Predictions
↓
Final Prediction
Five models predict:
Cat, Dog, Cat, Cat, Dog
Final prediction:
Cat
because Cat receives 3 of 5 votes.
Five models predict:
10, 12, 11, 15, 12
Final prediction:
\[ \frac{10+12+11+15+12}{5}=12 \]
Some models have high variance.
For example, a decision tree can change substantially when the training data changes slightly.
Bagging:
Averaging many unstable models can produce a more stable model.
Bagging primarily reduces variance.
A simplified view of prediction error is:
\[ \text{Error} \approx \text{Bias}^2 + \text{Variance} + \text{Noise} \]
Bagging primarily targets:
It generally does not dramatically reduce the bias of the base model.
This is why bagging is especially useful for flexible, high-variance models such as decision trees.
| Bootstrapping | Bagging |
|---|---|
| Resampling technique | Ensemble method |
| Creates datasets | Creates and combines models |
| Samples with replacement | Usually uses bootstrap samples |
| Can be used for statistical inference | Used for predictive modeling |
| Does not require ML models | Requires multiple models |
Bootstrapping creates the samples. Bagging uses those samples to build an ensemble.
from sklearn.ensemble import BaggingClassifier
from sklearn.tree import DecisionTreeClassifier
model = BaggingClassifier(
estimator=DecisionTreeClassifier(),
n_estimators=100,
random_state=42
)
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
n_estimators=100 means we train 100 base models.
Random Forest is a specialized form of bagging.
It combines:
Conceptually:
Training Data
↓
Bootstrap Samples
↓
Tree 1 Tree 2 Tree 3 ... Tree B
↓ ↓ ↓
└──────┴───────┴──────┘
↓
Aggregate Predictions
↓
Final Prediction
Random feature selection makes the trees less correlated, which can improve the ensemble.
from sklearn.ensemble import RandomForestClassifier
rf = RandomForestClassifier(
n_estimators=100,
random_state=42
)
rf.fit(X_train, y_train)
y_pred = rf.predict(X_test)
Random Forest = Bagged decision trees + random feature selection
A Random Forest can tell us which features were useful for prediction.
Example:
| Feature | Importance |
|---|---|
| Contract Length | 0.40 |
| Monthly Charges | 0.25 |
| Tenure | 0.20 |
| Age | 0.15 |
A high score means the model found the feature useful for prediction.
It does not mean the feature causes the outcome.
Scikit-learn’s default Random Forest importance is based on:
A feature receives importance when it produces useful tree splits.
For classification, one common impurity measure is Gini impurity:
\[ Gini = 1-\sum_{k=1}^{K}p_k^2 \]
A good split makes child nodes more homogeneous, reducing impurity.
The importance is aggregated across all trees.
Scikit-learn provides:
rf.feature_importances_
To associate importance with feature names:
import pandas as pd
importance = pd.Series(
rf.feature_importances_,
index=X_train.columns
).sort_values(ascending=False)
print(importance)
The values typically sum to 1.
import matplotlib.pyplot as plt
importance.sort_values().plot(
kind="barh"
)
plt.xlabel("Feature Importance")
plt.title("Random Forest Feature Importance")
plt.show()
This provides a quick view of which features the model relied on most.
MDI has limitations, especially with:
An alternative is permutation importance.
The idea:
Shuffle one feature and measure how much model performance decreases.
Example:
Original accuracy: 92%
Shuffle Age: 91%
Shuffle Contract Length: 72%
Contract Length causes a much larger performance drop, suggesting it is more important to the model’s predictions.
from sklearn.inspection import permutation_importance
result = permutation_importance(
rf,
X_test,
y_test,
n_repeats=10,
random_state=42
)
importance = pd.Series(
result.importances_mean,
index=X_test.columns
).sort_values(ascending=False)
print(importance)
Permutation importance is based on changes in model performance, not tree impurity.
| MDI | Permutation | |
|---|---|---|
| Scikit-learn | feature_importances_ |
permutation_importance() |
| Based on | Tree splits | Model performance |
| Measures | Impurity reduction | Performance decrease |
| Speed | Fast | Slower |
| Potential bias | Yes | Less affected by some MDI biases |
Remember
MDI asks:
How much did this feature help reduce impurity in the trees?
Permutation asks:
How much does model performance suffer when this feature is shuffled?
Suppose:
Income → importance = 0.40
We can conclude:
Income was useful for the model’s predictions.
We cannot conclude:
Income causes the outcome.
Feature importance describes the behavior of a predictive model, not a causal relationship.
Suppose the dataset contains:
Annual Salary
Monthly Salary
These features contain very similar information.
Importance may be distributed between them.
Therefore:
A low importance score does not necessarily mean a feature is unimportant.
Always consider relationships between features when interpreting importance.
Training Data
↓
Bootstrap Samples
↓
Multiple Decision Trees
↓
Random Feature Selection
↓
Random Forest
↓
Predictions
↓
Feature Importance
├── MDI
└── Permutation Importance
The overall goal is to build a model that is:
feature_importances_ provides MDI-based importanceA Random Forest gives:
Income → importance = 0.40
What can we conclude?
A. Income causes the outcome.
B. Income was useful for the model’s predictions.
C. Increasing income will increase the prediction.
D. Income is statistically significant.
Bootstrapping creates different datasets; bagging combines models to reduce variance; Random Forest extends bagging with random feature selection; and feature importance tells us what the model found useful—not what causes the outcome.