liveBook · Manning

BaggingClassifier

As explained in Machine Learning for Tabular Data: XGBoost, Deep Learning, and AI

BaggingClassifier is a scikit-learn ensemble estimator that fits base classifiers on bootstrap samples and aggregates their predictions.

BaggingClassifier is a scikit-learn ensemble estimator that trains several classifiers on resampled data and combines their predictions. Set estimator to choose the base model, n_estimators to control ensemble size, and bootstrap, max_samples, and max_features to control sampling. section 5.1.1

Use the sampling parameters to express four related ensemble strategies:

  • Bagging samples training examples with replacement, using bootstrap=True, max_samples=1.0, and max_features=1.0. section 5.1.1
  • Pasting samples examples without replacement; random subspaces sample features; random patches sample both examples and features. section 5.1.1
  • Choose decision trees by default or provide another weak or strong classifier through estimator. section 5.1.1

Bagged tree-based classifier

from sklearn.ensemble import BaggingClassifier from sklearn.tree import DecisionTreeClassifier model = BaggingClassifier( estimator=DecisionTreeClassifier(), n_estimators=300, bootstrap=True, max_samples=1.0, max_features=1.0, random_state=0 ) A five-fold shuffled evaluation of the complete workflow reports 0.809 accuracy, with approximately 37.93 seconds for fitting and 0.83 seconds for prediction. section 5.1.1

BaggingClassifier also supports repeated logistic-regression fits for stability selection. Bootstrap resamples and L1-regularized coefficients reveal features that remain relevant across repeated models, making the procedure useful for reducing high-dimensional feature sets before prediction. section 6.2.1

Table 5.1 Bagging and sampling strategies (view table figure)

Averaging strategy

What happens to data

Parameters for BaggingClassifier/BaggingRegressor

Pasting

Training examples are sampled without replacement

bootstrap = False

max_samples < 1.0

max_features = 1.0

Bagging

Training examples are sampled with replacement (bootstrapping)

bootstrap = True

max_samples = 1.0

max_features = 1.0

Random subspaces

Features are sampled (without replacement)

bootstrap = False

max_samples = 1.0

max_features < 1.0

Random patches

Training examples and features are sampled without replacement

bootstrap = False

max_samples < 1.0

max_features < 1.0

Compare the parameter settings for bagging, pasting, random subspaces, and random patches. section 5.1.1

How Machine Learning for Tabular Data: XGBoost, Deep Learning, and AI covers it

How deep it goes
Explains estimator choice and sampling controls with a worked classifier, then applies bootstrapping to feature-selection stability.
Examples
Uses a 300-tree decision-tree ensemble and repeated L1 logistic-regression coefficients to identify stable features.
What it assumes
Assumes familiarity with classification estimators and bootstrap resampling while treating sampling strategies through parameter settings.
What you can do after
Build it from scratch with scikit-learn by configuring a base estimator, ensemble size, sampling strategy, and random state.
How it connects
Connects bootstrap sampling, decision trees, random forests, random subspaces, random patches, logistic regression, and L1 regularization.

Also explained in

Compare how 2 books explain BaggingClassifier →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Machine Learning for Tabular Data: XGBoost, Deep Learning, and AI. The section links point to the parts of the book it draws on.