AlgoInsight

Q&A

What is the Bootstrap method in data science?

By AlgoInsight Team2026-06-18css

Overview

Classical statistics derives confidence intervals analytically, which often requires assumptions (normality, large samples). The bootstrap sidesteps these by simulating the sampling process: treat your observed sample as if it were the population, draw thousands of "bootstrap samples" of the same size with replacement, compute your statistic on each, and use the distribution of results as an empirical approximation of the true sampling distribution.

graph TD
    A[Original sample n=100] --> B["Draw 1000 bootstrap samples<br/>(size n, with replacement)"]
    B --> C["Compute statistic on each<br/>(mean, median, R², etc.)"]
    C --> D["Distribution of 1000 estimates"]
    D --> E["95% CI: 2.5th–97.5th percentile"]
    style D fill:#1e1b4b,color:#fff

How It Works

  1. Start with a sample of size n.
  2. Draw B bootstrap samples (typically 1000–10000), each of size n, with replacement - meaning each draw can include duplicates and some original observations are omitted.
  3. Compute the statistic of interest (mean, median, correlation, model accuracy, custom metric) on each bootstrap sample.
  4. The collection of B estimates is the bootstrap distribution.
  5. Derive a confidence interval - most commonly the percentile method: the 2.5th and 97.5th percentiles of the bootstrap distribution form the 95% CI.

Python Implementation

import numpy as np

rng = np.random.default_rng(42)
data = rng.normal(loc=5, scale=2, size=100)   # our observed sample

B = 5000
bootstrap_means = np.array([
    rng.choice(data, size=len(data), replace=True).mean()
    for _ in range(B)
])

ci_lower = np.percentile(bootstrap_means, 2.5)
ci_upper = np.percentile(bootstrap_means, 97.5)
print(f"Bootstrap 95% CI for mean: ({ci_lower:.3f}, {ci_upper:.3f})")
print(f"Bootstrap SE: {bootstrap_means.std():.3f}")

The replace=True argument to np.random.choice is what makes it bootstrap - sampling with replacement.

Non-Parametric Confidence Interval for Any Statistic

The same code works for any statistic - just change .mean():

bootstrap_medians = np.array([
    np.median(rng.choice(data, size=len(data), replace=True))
    for _ in range(B)
])
# Median's CI, even though no closed-form formula exists

This is the bootstrap's superpower: it applies to statistics that don't have a standard analytical CI (trimmed means, custom model metrics, AUC, etc.).

Scikit-learn Integration: Bootstrap for Model Evaluation

from sklearn.utils import resample
from sklearn.metrics import accuracy_score
from sklearn.ensemble import RandomForestClassifier

scores = []
for _ in range(200):
    X_boot, y_boot = resample(X_train, y_train)    # bootstrap sample
    model = RandomForestClassifier()
    model.fit(X_boot, y_boot)
    scores.append(accuracy_score(y_test, model.predict(X_test)))

print(f"Mean accuracy: {np.mean(scores):.3f}")
print(f"95% CI: ({np.percentile(scores,2.5):.3f}, {np.percentile(scores,97.5):.3f})")

Key Properties

Bootstrap vs Other Resampling

Method Sampling Used for
Bootstrap With replacement CI, SE for any statistic
Cross-validation Without replacement Model generalization estimate
Permutation test Without replacement (shuffled) Hypothesis testing

Why This Matters in Interviews

Data science and statistics interviews use bootstrap to test resampling fluency. Core points: draw samples with replacement; compute statistic on each; the distribution of results estimates the sampling distribution; use percentiles for CIs; it works for any statistic (no closed-form formula needed). Distinguishing bootstrap (with replacement, for uncertainty quantification) from cross-validation (without replacement, for model evaluation) is a frequent discriminator.

Common Pitfalls

References

More answers