Extending what CharlesG posted, here's my solution for overloading the BaggingRegressor (same should work for BaggingClassifier).
class myBaggingRegressor(BaggingRegressor):
def fit(self, X, y):
fitd = super().fit(X, y)
# need to pad features?
if self.max_features == 1.0:
# compute feature importances or coefficients
if hasattr(fitd.estimators_[0], 'feature_importances_'):
self.feature_importances_ = np.mean([est.feature_importances_ for est in fitd.estimators_], axis=0)
else:
self.coef_ = np.mean([est.coef_ for est in fitd.estimators_], axis=0)
self.intercept_ = np.mean([est.intercept_ for est in fitd.estimators_], axis=0)
else:
# need to process results into the right shape
coefsImports = np.empty(shape=(self.n_features_, self.n_estimators), dtype=float)
coefsImports.fill(np.nan)
if hasattr(fitd.estimators_[0], 'feature_importances_'):
# store the feature importances
for idx, thisEstim in enumerate(fitd.estimators_):
coefsImports[fitd.estimators_features_[idx], idx] = thisEstim.feature_importances_
# compute average
self.feature_importances_ = np.nanmean(coefsImports, axis=1)
else:
# store the coefficients & intercepts
self.intercept_ = 0
for idx, thisEstim in enumerate(fitd.estimators_):
coefsImports[fitd.estimators_features_[idx], idx] = thisEstim.coefs_
self.intercept += thisEstim.intercept_
# compute
self.intercept /= self.n_estimators
# average
self.coefs_ = np.mean(coefsImports, axis=1)
return fitd
This correctly handles if max_features <> 1.0
, though I suppose won't work exactly if bootstrap_features=True
.
I suppose it's because sklearn has evolved a lot since 2017, but couldn't get it to work with the constructor, and it doesn't seem entirely necessary - the only reason to have that is to pre-specify the feature_importances_ attribute as None. However, it shouldn't even exist until fit() is called anyway.