Bagging and variance
Quick answer
Bagging combines predictions from models fitted on resampled training sets. Averaging can reduce prediction variability when individual errors are not perfectly correlated; it cannot guarantee better generalization. Examples run locally with Python 3.13.5, scikit-learn 1.8.0, NumPy 2.3.5 and SciPy 1.17.0 when imported. ByteMind library execution is unverified.
Compare hard and soft votes
from fractions import Fraction as Fp=[F(51,100),F(51,100),F(1,100)]print(sum(x>=F(1,2) for x in p))print(sum(p)/len(p))Expected output:
2103/300Hard voting is positive but soft voting at 0.5 is negative.
How it works
For regression, a bagged prediction is the arithmetic mean of member predictions. For classification, hard voting counts labels; soft voting averages class probability estimates in a common class order. They can disagree: three binary models with positive probabilities 0.51, 0.51, 0.01 give two positive hard votes but a mean below 0.5. The variance of the mean of B equal-variance random predictions with pairwise correlation rho is sigma²[rho+(1−rho)/B]. This follows by adding B variance terms and B(B−1) covariance terms, then dividing by B². Here the randomness is over fitting samples or model randomness at a fixed input; it is not the variance of labels. With variance 9 and rho=0, nine members yield variance 1; with rho=1, the variance remains 9. Bias, correlation and distribution shifts can limit gains. Training and inference costs generally grow with member count; independent members can be parallelized subject to memory and CPU limits. Compare models using identical validation rows and a declared metric.
Average a bagged regression prediction
from sklearn.ensemble import BaggingRegressorfrom sklearn.tree import DecisionTreeRegressorimport numpy as npX=[[0],[1],[2],[3]]; y=[0,2,4,6]m=BaggingRegressor(estimator=DecisionTreeRegressor(),n_estimators=5,random_state=7,n_jobs=1).fit(X,y)manual=np.mean([t.predict([[1.5]])[0] for t in m.estimators_])print(bool(np.isclose(manual,m.predict([[1.5]])[0])))Expected output:
TrueAll columns are available in this fixture, so the manual average matches the ensemble.
Common mistakes
Do not average class IDs as if numeric distances were meaningful. More members are not a cure for systematic bias or leakage. Do not confuse correlated models with independent predictions.
Practice and next steps
Compute the variance of an average of four members with variance 8 and pairwise correlation 0.5. Compare against a single member. Complete the knowledge check and coding exercise. Next: Random feature subsets.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Bagging and variance — knowledge check
Apply bagging and variance to new examples.
What is the mean of regression predictions 2, 5, 8?
What happens to variance for perfectly correlated equal-variance members?
Can hard and soft votes disagree?
For independent members of variance 12 and B=3, what is mean variance?
What may increase as member count grows?