0
0
MlopsComparisonBeginner · 4 min read

Bagging vs Boosting vs Stacking in Python: Key Differences & Code

In Python, bagging builds multiple independent models on random data samples and averages their results, boosting builds models sequentially to correct previous errors, and stacking combines different model predictions using a meta-model for better accuracy. All three are ensemble methods but differ in how they train and combine base models.
⚖️

Quick Comparison

Here is a quick table comparing bagging, boosting, and stacking on key factors.

FactorBaggingBoostingStacking
Training StyleParallel, independent modelsSequential, models learn from errorsParallel base models + meta-model
GoalReduce varianceReduce bias and varianceCombine strengths of different models
Model DependencyModels are independentModels depend on previous onesBase models independent, meta-model dependent
Common AlgorithmsRandom ForestAdaBoost, Gradient BoostingAny classifiers/regressors
Risk of OverfittingLower riskHigher risk if too many roundsDepends on base and meta models
Use CaseWhen data is noisyWhen model needs improvement on errorsWhen combining diverse models
⚖️

Key Differences

Bagging (Bootstrap Aggregating) trains multiple copies of the same model on different random subsets of data. Each model votes or averages predictions, which reduces variance and helps with noisy data. Models are trained independently and in parallel.

Boosting trains models one after another. Each new model focuses on the mistakes of the previous ones, improving overall accuracy by reducing bias and variance. This sequential dependency can lead to better performance but risks overfitting if not controlled.

Stacking combines predictions from multiple different models (base learners) by training a new model (meta-learner) on their outputs. This approach leverages the strengths of diverse models and can improve accuracy beyond bagging or boosting alone. It requires careful training to avoid data leakage.

💻

Code Comparison: Bagging Example

This example shows how to use BaggingClassifier with a decision tree on the Iris dataset.

python
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import BaggingClassifier
from sklearn.metrics import accuracy_score

# Load data
iris = load_iris()
X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target, random_state=42)

# Base model
base_clf = DecisionTreeClassifier(random_state=42)

# Bagging ensemble
bagging_clf = BaggingClassifier(base_estimator=base_clf, n_estimators=10, random_state=42)
bagging_clf.fit(X_train, y_train)

# Predict and evaluate
preds = bagging_clf.predict(X_test)
accuracy = accuracy_score(y_test, preds)
print(f"Bagging accuracy: {accuracy:.3f}")
Output
Bagging accuracy: 0.974
↔️

Boosting Equivalent

This example uses AdaBoostClassifier with a decision tree on the same Iris dataset.

python
from sklearn.ensemble import AdaBoostClassifier

# Base model
base_clf = DecisionTreeClassifier(max_depth=1, random_state=42)

# Boosting ensemble
boosting_clf = AdaBoostClassifier(estimator=base_clf, n_estimators=50, random_state=42)
boosting_clf.fit(X_train, y_train)

# Predict and evaluate
preds = boosting_clf.predict(X_test)
accuracy = accuracy_score(y_test, preds)
print(f"Boosting accuracy: {accuracy:.3f}")
Output
Boosting accuracy: 0.974
🎯

When to Use Which

Choose bagging when your main goal is to reduce variance and handle noisy data, especially with unstable models like decision trees.

Choose boosting when you want to reduce bias and improve model accuracy by focusing on hard-to-predict samples, but watch out for overfitting.

Choose stacking when you want to combine different types of models to leverage their strengths and achieve better overall performance, especially in complex problems.

Key Takeaways

Bagging builds independent models on random data samples to reduce variance.
Boosting builds models sequentially to correct previous errors and reduce bias.
Stacking combines diverse model predictions using a meta-model for improved accuracy.
Use bagging for noisy data, boosting for improving weak models, and stacking for combining strengths.
All three are ensemble methods but differ in training style and model dependency.