Skip to content

sklearn/manual-bootstrap-loop

Pack Severity Fix Versions
scikit-learn info none, edit by hand All versions

A loop that draws a bootstrap sample of the rows and refits on it once per pass fits the resampled models one after another. BaggingRegressor and BaggingClassifier fit the same resampled clones in parallel.

models = []
for b in range(200):
idx = rng.choice(len(y), len(y))
models.append(clone(est).fit(X[idx], y[idx]))
bag = BaggingRegressor(est, n_estimators=200, bootstrap=True, n_jobs=-1, random_state=0).fit(X, y)
models = bag.estimators_

The rule reports, as info, a loop over range(...) that assigns a row sample from choice, integers, randint, resample or df.sample(replace=True) and passes it to fit. A bootstrap of a whole pipeline or of a metric is often written this way on purpose, so the severity is info.

xpd has no automatic fix, so replace the loop by hand.

  • BaggingRegressor or BaggingClassifier(est, n_estimators=..., bootstrap=True, n_jobs=-1) keeps the clones in estimators_ and the rows each drew in estimators_samples_.
  • For a confidence interval of a statistic, scipy.stats.bootstrap(data, statistic, vectorized=True).
This refits on one bootstrap sample of rows $IDX per pass. BaggingRegressor or BaggingClassifier(est, n_estimators=n_boot, bootstrap=True, n_jobs=-1) fits the same resampled clones in parallel, with the rows of each in estimators_samples_, and scipy.stats.bootstrap(data, statistic, vectorized=True) gives a confidence interval of a statistic without the loop.

See all rules in the scikit-learn pack.