Implementation:Scikit learn contrib Imbalanced learn BalancedBaggingClassifier
Appearance
| Knowledge Sources | |
|---|---|
| Domains | Machine_Learning, Ensemble_Learning, Imbalanced_Learning |
| Last Updated | 2026-02-09 03:00 GMT |
Overview
Concrete tool for balanced bagging classification provided by the imbalanced-learn library.
Description
The BalancedBaggingClassifier extends sklearn.ensemble.BaggingClassifier with an additional resampling step per bag. The sampler parameter accepts any imblearn sampler (default: RandomUnderSampler). Supports all standard bagging parameters.
Usage
Import this class as a drop-in replacement for BaggingClassifier when balancing is needed. Use the sampler parameter to plug in SMOTE, NearMiss, or any other imblearn sampler.
Code Reference
Source Location
- Repository: imbalanced-learn
- File: imblearn/ensemble/_bagging.py
- Lines: L29-366
Signature
class BalancedBaggingClassifier(BaggingClassifier):
def __init__(
self,
estimator=None,
n_estimators=10,
*,
max_samples=1.0,
max_features=1.0,
bootstrap=True,
bootstrap_features=False,
oob_score=False,
warm_start=False,
sampling_strategy="auto",
replacement=False,
n_jobs=None,
random_state=None,
verbose=0,
sampler=None,
):
Import
from imblearn.ensemble import BalancedBaggingClassifier
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| X | {array-like, sparse matrix} of shape (n_samples, n_features) | Yes | Training features |
| y | array-like of shape (n_samples,) | Yes | Target labels |
| sampler | sampler object or None | No | Sampler for per-bag balancing (default: RandomUnderSampler) |
Outputs
| Name | Type | Description |
|---|---|---|
| fit() returns | self | Fitted ensemble |
| predict() returns | ndarray of shape (n_samples,) | Predicted labels |
Usage Examples
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from imblearn.ensemble import BalancedBaggingClassifier
X, y = make_classification(n_classes=2, weights=[0.1, 0.9], n_samples=1000, random_state=0)
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)
bbc = BalancedBaggingClassifier(n_estimators=10, random_state=0)
bbc.fit(X_train, y_train)
print(f"Score: {bbc.score(X_test, y_test):.3f}")
Related Pages
Implements Principle
Requires Environment
Uses Heuristic
Page Connections
Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment