Fatskills
Practice. Master. Repeat.
Study Guide: Data Science and Machine Learning 101: Machine Learning Core Overfitting and Underfitting BiasVariance Tradeoff Regularization Early Stopping
Source: https://www.fatskills.com/data-science/chapter/data-science-and-machine-learning-data-science-and-machine-learning-machine-learning-core-overfitting-and-underfitting-biasvariance-tradeoff-regularization-early-stopping

Data Science and Machine Learning 101: Machine Learning Core Overfitting and Underfitting BiasVariance Tradeoff Regularization Early Stopping

By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.

⏱️ ~5 min read

What This Is

Overfitting happens when a model learns the noise and the signal in the training data, so it performs great on that data but poorly on new, unseen examples. Underfitting is the opposite: the model is too simple to capture the underlying pattern, yielding mediocre performance everywhere. The bias‑variance trade‑off formalizes this tension—high bias (under‑fit) + low variance vs. low bias (over‑fit) + high variance. In a real‑world pipeline—say a churn‑prediction model for a telecom provider—over‑fitting would let you “predict” churn perfectly on last month’s data but miss the next month’s churners, while under‑fitting would give you a bland 50 % accuracy that never improves, no matter how much data you add.


Key Terms & Formulas

  • Bias – Systematic error from erroneous assumptions in the learning algorithm (e.g., linear model on a highly non‑linear problem).
  • Variance – Sensitivity of the model to small fluctuations in the training set; high variance = model changes a lot with different data splits.
  • Expected Generalization Error: (\displaystyle \mathbb{E}{\mathcal{D}}[(y - \hat{f}(x))^2] = \underbrace{\text{Bias}^2}}} + \underbrace{\text{Var}{\text{model\,instability}} + \underbrace{\sigma^2}). }
  • L2 Regularization (Ridge): (\displaystyle J_{\text{ridge}}(w) = \frac{1}{N}\sum_{i=1}^N L(y_i,\hat{y}_i) + \lambda |w|_2^2). Penalizes large weights → reduces variance.
  • L1 Regularization (Lasso): (\displaystyle J_{\text{lasso}}(w) = \frac{1}{N}\sum_{i=1}^N L(y_i,\hat{y}_i) + \lambda |w|_1). Drives weights to exact zero → performs feature selection.
  • Early Stopping – Stop iterative training (e.g., SGD, boosting) when validation loss stops improving for p epochs:

python if val_loss > best_val_loss * (1 + tol):
patience += 1 else:
best_val_loss = val_loss
patience = 0 if patience >= max_patience: break


  • Cross‑Validation (k‑fold) – Split data into k folds, train on k‑1, validate on the held‑out fold; average the metric to estimate variance.
  • Dropout (Neural Nets) – Randomly zero out a proportion p of activations each forward pass:

python mask = (np.random.rand(*layer.shape) > p).astype(float) layer_out = layer_out * mask / (1-p)


  • Bias‑Variance Decomposition (Monte‑Carlo) – Approximate bias & variance by repeatedly training on bootstrap samples and measuring predictions on a fixed test set.
  • Regularization Path – Vary λ (e.g., np.logspace(-4, 2, 100)) and plot training vs. validation error to spot the “elbow” where over‑fitting begins.


Step‑by‑Step / Process Flow

  1. Load & Split – Read data with pandas, do a stratified train_test_split (e.g., test_size=0.2).
  2. Baseline Model – Fit a simple algorithm (e.g., LogisticRegression with default C=1.0) and record train/validation scores.
  3. Diagnose – Compare training vs. validation error:
  4. Training ≈ Validation → low bias, low variance (good).
  5. Training >> Validation → high variance (over‑fit).
  6. Both low → high bias (under‑fit).
  7. Apply Regularization / Complexity Control
  8. For linear models: tune C (inverse of λ) with GridSearchCV.
  9. For trees/boosting: limit max_depth, min_samples_leaf, or add subsample.
  10. For NNs: add Dropout, L2 weight decay, or enable EarlyStopping.
  11. Re‑evaluate – Re‑run cross‑validation; plot learning curves (train_size vs. error) to see if the gap narrows.
  12. Finalize & Deploy – Choose the λ/complexity that gives the smallest validation error, retrain on the full training set, and export the model (e.g., joblib.dump).

Common Mistakes

Mistake Correction
Using the test set for hyper‑parameter tuning – leaks information and hides over‑fitting. Keep a validation split (or use k‑fold CV) for tuning; reserve the test set for the final unbiased estimate.
Setting λ too high – L1/L2 penalty kills all coefficients, yielding under‑fit. Start with a small λ (e.g., 0.001) and increase gradually; inspect the regularization path to avoid the “all‑zero” region.
Stopping too early – Early stopping after just one epoch may halt before the model has learned anything. Use a patience of 5–10 epochs and monitor a smoothed validation loss; also set a minimum number of epochs.
Ignoring data scaling – Regularization assumes comparable feature scales; unscaled features cause the penalty to affect some coefficients more. Apply StandardScaler (or RobustScaler for outliers) before fitting regularized models.
Assuming dropout works for all models – Dropout is only for deep nets; applying it to linear models has no effect. Use dropout only in architectures with hidden layers; for linear models rely on L1/L2.


Data Science Interview / Practical Insights

  1. “Explain the bias‑variance trade‑off and how regularization moves you along it.” – Expect you to draw the classic U‑shaped test error curve and say L2 reduces variance at the cost of a bit more bias.
  2. “When would you prefer L1 over L2?” – L1 when you need sparse models (feature selection) or interpretability; L2 when you care about stability and all features are potentially useful.
  3. “How does early stopping differ from a validation‑set‑based hyper‑parameter search?” – Early stopping is a training‑time regularizer that halts gradient descent; CV searches over static hyper‑parameters after training.
  4. “What’s the danger of using a single train/validation split on a small dataset?” – High variance in the estimate; you should use k‑fold CV or repeated CV to get a reliable picture of over‑/under‑fitting.

Quick Check Questions

  1. Scenario: Your model’s training accuracy is 99 % but validation accuracy is 70 %. What do you try first?
    Answer: Increase regularization (e.g., raise λ or add dropout) to curb variance.
  2. Scenario: After adding L2 regularization, both training and validation errors rise slightly, but the gap shrinks. What does this indicate?
    Answer: You’ve moved toward a better bias‑variance balance—accepting a bit more bias for much lower variance.
  3. Scenario: You observe that validation loss stops improving after epoch 12 and then slowly rises. Which technique should you employ?
    Answer: Early stopping with patience ≈ 3–5 epochs to freeze the model at epoch 12.

Last‑Minute Cram Sheet (10 one‑liners)

  1. Bias + Variance + Irreducible Noise = Expected Test Error – the core of the trade‑off.
  2. L2 penalty = λ·∑w² – shrinks weights uniformly, reduces variance.
  3. L1 penalty = λ·∑|w| – forces exact zeros → built‑in feature selection.
  4. EarlyStopping → monitor val_loss; patience = # epochs without improvement.
  5. Dropout rate p = probability of zeroing a neuron; scale by 1/(1‑p) at train time.
  6. k‑fold CV (k≈5‑10) gives a low‑variance estimate of model performance.
  7. Learning curves: if training error ↓ and validation error ↑ → over‑fit; both ↑ → under‑fit.
  8. ⚠️ Scaling matters: L1/L2 assume features on comparable scales; always StandardScaler or RobustScaler.
  9. Regularization path “elbow” = λ where validation error stops decreasing – pick that λ.
  10. ⚠️ Test set is never used for hyper‑parameter tuning; it’s only for the final performance report.


ADVERTISEMENT