Bagging (Bootstrap Aggregating) ensemble implementations for classification and regression.
Classes
BaggingClassifier for bootstrap aggregating ensemble classification.
__init__( self, base_classifier: Any = 'DecisionTreeClassifier', n_estimators: int = 10, bag_size_percent: int = 100, random_state: Optional[int] = None, n_jobs: int = 1, )
Overview
The algorithm proceeds as follows:
- For each of T ensemble members:
- To predict, aggregate predictions from all T classifiers via majority vote
Theory
Each bootstrap sample S_t is drawn with replacement from the original dataset D of size n:
where n' = n \cdot \text{bag\_size\_percent} / 100.
The final ensemble prediction uses majority voting:
The variance reduction from bagging is:
where \rho is the pairwise correlation between base learners and \sigma^2 is the variance of a single base learner.
Parameters
base_classifier
n_estimators
bag_size_percent
random_state
n_jobs
-1 means use all available processors.
Attributes
estimators_
classes_
fit().
Notes
Complexity:
- Training: O(T \cdot n' \cdot C_{\text{base}}) where T = n_estimators,
- Prediction: O(T \cdot C_{\text{predict}}) per sample
- When the base learner is unstable (high variance), such as decision trees
- When you want to reduce overfitting without increasing bias
- When parallel training is desirable (each estimator is independent)
- As a building block for more complex ensemble methods (e.g., Random Forest)
References
Basic usage for classification with bootstrap aggregating:
>>> from tuiml.algorithms.ensemble import BaggingClassifier
>>> import numpy as np
>>>
>>> # Create sample training data
>>> X_train = np.array([[1, 2], [2, 3], [3, 1], [4, 3], [5, 2]])
>>> y_train = np.array([0, 0, 1, 1, 1])
>>>
>>> # Fit the Bagging classifier
>>> clf = BaggingClassifier(base_classifier='DecisionTreeClassifier', n_estimators=10)
>>> clf.fit(X_train, y_train)
BaggingClassifier(...)
>>> predictions = clf.predict(X_train)
Methods
get_parameter_schema
(cls) -> Dict[str, Dict[str, Any]]
get_capabilities
(cls) -> List[str]
get_complexity
(cls) -> str
get_references
(cls) -> List[str]
__repr__
(self) -> str
BaggingRegressor for bootstrap aggregating ensemble regression.
__init__( self, base_regressor: Any = 'GradientBoostingRegressor', n_estimators: int = 10, bag_size_percent: int = 100, random_state: Optional[int] = None, n_jobs: int = 1, )
Overview
The algorithm proceeds as follows:
- For each of T ensemble members:
- To predict, average predictions from all T regressors
Theory
Each bootstrap sample S_t is drawn with replacement from the original dataset D of size n:
where n' = n \cdot \text{bag\_size\_percent} / 100.
The final ensemble prediction averages across all base regressors:
The variance reduction from bagging is:
where \rho is the pairwise correlation between base learners and \sigma^2 is the variance of a single base learner.
Parameters
base_regressor
n_estimators
bag_size_percent
random_state
n_jobs
-1 means use all available processors.
Attributes
estimators_
Notes
Complexity:
- Training: O(T \cdot n' \cdot C_{\text{base}}) where T = n_estimators,
- Prediction: O(T \cdot C_{\text{predict}}) per sample
- When the base learner is unstable (high variance), such as decision trees
- When you want to reduce overfitting without increasing bias
- When parallel training is desirable (each estimator is independent)
- When prediction averaging can smooth out individual model noise
References
See Also
Basic usage for regression with bootstrap aggregating:
>>> from tuiml.algorithms.ensemble import BaggingRegressor
>>> import numpy as np
>>>
>>> # Create sample training data
>>> X_train = np.array([[1, 2], [2, 3], [3, 1], [4, 3], [5, 2]])
>>> y_train = np.array([1.5, 2.3, 3.1, 4.2, 5.0])
>>>
>>> # Fit the Bagging regressor
>>> reg = BaggingRegressor(n_estimators=10, random_state=42)
>>> reg.fit(X_train, y_train)
BaggingRegressor(...)
>>> predictions = reg.predict(X_train)