Skip to content
E
Educatum

Bagging VS boosting

Curated by Educatum AI teamLast updated:
Basic Concepts
Direct Answer

Bagging and boosting are ensemble methods that combine multiple base models into a stronger predictor, but they target opposite sources of error. Bagging trains low-bias, high-variance learners independently in parallel on bootstrapped subsets and averages their outputs to reduce variance, whereas boosting trains weak learners sequentially—each correcting errors or residuals from earlier models—to primarily reduce bias.

Study Card: Bagging VS boosting

Direct Answer

Bagging and boosting are ensemble methods that combine multiple base models into a stronger predictor, but they differ fundamentally in how models are trained and which error component they target. Bagging (Bootstrap Aggregating) trains high-variance, low-bias learners (such as deep, unpruned decision trees) independently in parallel on bootstrapped datasets and aggregates their predictions by averaging or voting, primarily reducing variance. Boosting trains weak learners (such as shallow decision stumps) sequentially, where each new model focuses on correcting the mistakes or negative-gradient residuals of the existing ensemble, primarily reducing bias.

Key Terms

  • Ensemble Method: A machine learning approach that combines multiple base estimators to achieve better predictive accuracy or stability than any single model.
  • Bootstrap Aggregating (Bagging): An ensemble method that draws multiple training datasets via sampling with replacement (bootstrapping), trains base learners in parallel, and aggregates predictions via averaging (regression) or voting (classification).
  • Boosting: A sequential ensemble method where each weak learner improves upon the ensemble’s prior errors—either by reweighting misclassified training examples (AdaBoost) or fitting pseudo-residuals (Gradient Boosting).
  • Bias: Error from overly restrictive model assumptions that prevent capturing the true underlying pattern (underfitting).
  • Variance: Error from sensitivity to random fluctuations in the training sample (overfitting).

Example

Consider classifying images or tabular records with decision trees:

  • Bagging: We create 100 bootstrap samples by sampling rows with replacement and train an unpruned decision tree independently on each sample. To classify a new example, all 100 trees predict independently and we take the majority vote (or average predicted probabilities). Averaging across diverse trees smooths out sensitivity to individual outliers and reduces variance.
  • Boosting: We start with a shallow decision stump (high bias, low variance). In AdaBoost, misclassified examples receive higher sample weights so the next stump focuses on harder cases; in Gradient Boosting, each new shallow tree fits the negative gradient (pseudo-residuals) of the loss function. The combined prediction sums the weighted outputs of all sequential learners, driving down bias and fitting complex decision boundaries.

Code Implementation

import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.ensemble import BaggingClassifier, AdaBoostClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score
from sklearn.datasets import load_wine

# Load sample dataset
data = load_wine()
X = pd.DataFrame(data.data, columns=data.feature_names)
y = data.target
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.2, random_state=42, stratify=y
)

# Bagging: parallel ensemble of unpruned decision trees
bagging_clf = BaggingClassifier(
    estimator=DecisionTreeClassifier(random_state=42),
    n_estimators=50,
    random_state=42
)  # 50 decision trees aggregated via averaged predicted probabilities
bagging_clf.fit(X_train, y_train)
bagging_predictions = bagging_clf.predict(X_test)
bagging_accuracy = accuracy_score(y_test, bagging_predictions)

# Boosting (AdaBoost): sequential ensemble of weak learners (depth-1 stumps by default)
boosting_clf = AdaBoostClassifier(n_estimators=50, random_state=42)
boosting_clf.fit(X_train, y_train)
boosting_predictions = boosting_clf.predict(X_test)
boosting_accuracy = accuracy_score(y_test, boosting_predictions)

print(f"Bagging Accuracy: {bagging_accuracy:.4f}")
print(f"Boosting Accuracy: {boosting_accuracy:.4f}")
  • Bias-Variance Tradeoff: Bagging reduces variance without appreciably increasing bias; boosting primarily reduces bias, though excessive rounds or noisy labels can cause boosting to overfit and raise variance.
  • Random Forests: Extends bagging for decision trees by sampling a random subset of features (max_features) at each split, which lowers inter-tree correlation ρ\rho and further shrinks ensemble variance ρσ2+1−ρBσ2\rho \sigma^2 + \frac{1-\rho}{B}\sigma^2.
  • Gradient Boosting Machines (GBM / XGBoost / LightGBM): Generalize boosting to arbitrary differentiable loss functions by fitting weak learners to negative gradients with shrinkage (learning rate) and regularization.
  • Stacking: Combines heterogeneous base models (e.g., trees, linear models, SVMs) by training a meta-learner on their out-of-fold predictions rather than simple averaging or sequential boosting.
View all →

Try keywords like RoPE, FlashAttention, DPO, or Blind 75.