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

