iwantcoding.com
🔥 Daily 👥 Rooms 🏆 Top Log in Sign up

Over / Underfitting

Overfitting = your model memorised the training data. High train accuracy, low test accuracy. The fix is more data, fewer parameters, regularisation, dropout, early stopping, or cross-validation.

Detect + diagnose + cure

EXAMPLE
from sklearn.datasets       import make_classification
from sklearn.model_selection import train_test_split, learning_curve
from sklearn.linear_model    import LogisticRegression
import numpy as np
import matplotlib.pyplot as plt

X, y = make_classification(n_samples=200, n_features=20, n_informative=5, random_state=0)
Xtr, Xte, ytr, yte = train_test_split(X, y, test_size=0.3, random_state=0)

# 1) Detect — gap between train and val scores
for C in [0.01, 0.1, 1, 10, 100]:
    clf = LogisticRegression(C=C, max_iter=500).fit(Xtr, ytr)
    print(f'C={C:6.2f}  train {clf.score(Xtr, ytr):.3f}  val {clf.score(Xte, yte):.3f}')

# 2) Learning curve — does adding data help?
sizes, train_scores, val_scores = learning_curve(
    LogisticRegression(C=10, max_iter=500), Xtr, ytr,
    cv=5, train_sizes=np.linspace(0.1, 1.0, 5),
)
plt.plot(sizes, train_scores.mean(axis=1), label='train')
plt.plot(sizes, val_scores.mean(axis=1),   label='val')
plt.legend(); plt.show()

# 3) Cures — pick the smallest hammer that works
#   a) MORE DATA — biggest fix, biggest cost.
#   b) LESS CAPACITY — smaller model (fewer features, lower depth, smaller hidden).
#   c) REGULARISATION — L1 / L2 penalties on weights.
LogisticRegression(C=0.1, penalty='l2')        # small C = strong regularisation

#   d) DROPOUT — random unit-drop in neural nets.
import torch.nn as nn
nn.Sequential(nn.Linear(20, 64), nn.ReLU(), nn.Dropout(0.5), nn.Linear(64, 1))

#   e) EARLY STOPPING — train less.
from tensorflow.keras.callbacks import EarlyStopping
model.fit(Xtr, ytr, validation_split=0.2,
          callbacks=[EarlyStopping(patience=5, restore_best_weights=True)])

#   f) CROSS-VALIDATION — pick hyperparams without peeking at the test set.
from sklearn.model_selection import GridSearchCV
GridSearchCV(LogisticRegression(max_iter=500),
             {'C': [0.01, 0.1, 1, 10]}, cv=5).fit(Xtr, ytr).best_params_

Why it matters

Most overfitting is fixed by “more data + smaller model + regularisation” in that order. Skipping the diagnosis and reaching for an exotic technique is how teams stay stuck for weeks.

Tip: Tweak the snippet with Try it Yourself », then sit the quiz at the bottom of the page.

Example

Example
# Symptoms: train acc 99%, test acc 70%.
# Fixes: cross-validation, regularisation (L1/L2), dropout, more data, fewer features, early stopping.
Try it Yourself »

Test yourself

Q1. Overfitting is when…
Q2. A common fix is…
Q3. Early stopping helps because…

Discussion

Loading…