Step 2 of 6

Bias-variance & model evaluation

Why models fail: underfitting vs. overfitting, cross-validation, precision/recall/F1, ROC curves, and the metrics that actually matter in production.

14 min read

A model that performs perfectly on training data and poorly on new data hasn't learned anything useful - it's memorized the noise. Understanding why models fail is as important as building them.

The bias-variance tradeoff#

Every model's prediction error decomposes into three parts:

Error=Bias2+Variance+σnoise2\text{Error} = \text{Bias}^2 + \text{Variance} + \sigma^2_{\text{noise}}

Total error = bias² + variance + irreducible noise. You can trade bias for variance, but never eliminate the noise.

  • Bias: error from wrong assumptions. A linear model fit to curved data has high bias - it systematically misses the true pattern.
  • Variance: error from sensitivity to the training set. A complex model that fits training data perfectly changes dramatically with different samples - it's modeling noise.
  • Irreducible noise: randomness inherent in the data. No model can do better than this.
Low varianceHigh variance
Low biasThe sweet spot ✓Overfitting: memorizes noise
High biasUnderfitting: misses the patternWorst of both worlds

Underfitting vs. overfitting#

Underfitting (high bias): the model is too simple to capture the true relationship. Signs: poor performance on both training and validation data. Fix: use a more expressive model, add features, reduce regularization.

Overfitting (high variance): the model fits the training data too closely, including its noise. Signs: excellent training performance, poor validation performance. Fix: more data, regularization, simpler model, dropout, early stopping.

  1. 01Train the model
  2. 02If training loss is high → underfitting (increase capacity)
  3. 03If training loss is low but validation loss is high → overfitting (add regularization)
  4. 04If both are low → good fit (evaluate on test set)

Train / validation / test splits#

Never evaluate on data you trained on. The standard protocol:

SplitPurposeTypical size
Training setFit the model parameters60-80%
Validation setTune hyperparameters, select model10-20%
Test setFinal, unbiased performance estimate10-20%

The test set is touched once - after all decisions are made. If you tune hyperparameters on the test set, it becomes a second validation set and your reported performance is optimistic.

Cross-validation#

When data is limited, k-fold cross-validation makes better use of it: split data into k folds, train on k−1, validate on the held-out fold, rotate, and average. Every data point gets used for both training and validation.

  1. 01Split data into k folds (typically k = 5 or 10)
  2. 02For each fold: train on the other k−1 folds, evaluate on this fold
  3. 03Average the k evaluation scores
  4. 04Report mean ± standard deviation

Stratified k-fold preserves the class distribution in each fold - critical for imbalanced datasets where one class is rare.

k-fold cross-validation
Splits data into k equal parts, trains on k−1 and tests on the remaining fold, rotating through all k - gives a more reliable performance estimate.
Stratified sampling
Ensures each fold (or split) has the same class proportions as the full dataset - prevents a rare class from being missing in a fold.
Hyperparameter
A setting chosen before training (learning rate, k in k-fold, regularization strength) - tuned on validation data, not learned from training data.

Classification metrics#

Accuracy alone is misleading. On a dataset with 99% negative examples, a model that always predicts "negative" gets 99% accuracy while being completely useless.

MetricFormulaWhen to use
PrecisionTP / (TP + FP)When false positives are costly (spam filter)
RecallTP / (TP + FN)When false negatives are costly (disease screening)
F1 score2 · P · R / (P + R)When you need a single number balancing precision and recall
AUC-ROCArea under ROC curveWhen you want to evaluate across all thresholds

The ROC curve#

The ROC curve plots true positive rate (recall) vs. false positive rate as you vary the classification threshold. A perfect classifier hugs the top-left corner (TPR = 1, FPR = 0). A random classifier follows the diagonal.

AUC (area under the curve) summarizes the ROC in one number: 1.0 is perfect, 0.5 is random, and anything below 0.5 means your model is worse than random (flip its predictions).

In ML: AUC is threshold-independent - it measures how well the model ranks positive examples above negative ones, regardless of where you set the cutoff. This makes it ideal for comparing models before choosing a deployment threshold.

AUC-ROC
Area Under the Receiver Operating Characteristic curve - summarizes a classifier's ability to distinguish classes across all thresholds. 1.0 = perfect, 0.5 = random.
Precision
Of everything the model predicted positive, how many actually were - high precision means few false alarms.
Recall (sensitivity)
Of all actual positives, how many the model caught - high recall means few missed cases.
F1 score
The harmonic mean of precision and recall - a single number that balances both, useful when classes are imbalanced.

Regression metrics#

MetricWhat it measures
MSEAverage squared error - penalizes large errors heavily
RMSE√MSE - same units as the target
MAEAverage absolute error - robust to outliers
R²Proportion of variance explained - 1.0 is perfect, 0 is no better than predicting the mean

R² is intuitive but has a trap: it always increases (or stays the same) as you add more features, even irrelevant ones. Adjusted R² penalizes for the number of features and gives a fairer comparison.

Where it shows up in ML#

Early stopping is regularization

Stop training when validation loss starts increasing - you're limiting how much the model can memorize.

Ensemble methods reduce variance

Random forests average many high-variance trees, reducing variance without increasing bias.

Learning curves diagnose problems

Plot training and validation loss vs. training set size: converging curves = high bias; diverging = high variance.

Class imbalance distorts metrics

Use precision/recall, F1, or AUC-ROC instead of accuracy. Stratified sampling ensures minority classes are represented.

Further reading

Search Data Science After Dark

Search across prompts, models, papers, guides, and library topics.