Skip to content
mentoor

Topic 4 of 5 · 5 min read

Overfitting versus underfitting in practice

Goal: Diagnose overfitting or underfitting from training and validation scores and pick a fitting remedy for each.

Overfitting means the model has learned the noise in its training data and fails on new rows; underfitting means it is too simple to learn the pattern at all. You tell them apart by comparing the training score with the validation score.

Check yourself · 1 of 3

A model scores 0.99 on training data and 0.72 on validation data. What is the most likely diagnosis?

Choose one answer

Why it matters

A model with a very high training score looks like a success until someone scores it on fresh data. Without the comparison you cannot tell whether to collect more data, simplify the model or make it more flexible, and the wrong remedy makes things worse.

How to diagnose

Score the same model on both splits and read the two numbers together:

Training Validation Diagnosis
High Much lower Overfitting (high variance)
Low Low, close to training Underfitting (high bias)
High High, close together Good fit
Low Higher than training Suspect a bug or a lucky split

A learning curve, scores plotted against the amount of training data, adds more. If the validation score is still climbing when you run out of data, more data will help. If both curves have flattened at a poor level, more data will not help; the model needs more capacity or better features. The scikit-learn guide to validation and learning curves shows how to plot these.

Remedies

  • For overfitting: get more data, remove or combine features, simplify the model (a shallower tree, fewer layers), add regularization (in scikit-learn's LogisticRegression a smaller C means stronger regularization), or use early stopping.
  • For underfitting: add informative features, use a more flexible model, or reduce regularization.

Change one thing at a time and re-read both scores.

Before and after

from sklearn.tree import DecisionTreeClassifier

deep = DecisionTreeClassifier(random_state=0).fit(X_train, y_train)
print(deep.score(X_train, y_train), deep.score(X_val, y_val))   # e.g. 1.00 vs 0.71

small = DecisionTreeClassifier(max_depth=4, random_state=0).fit(X_train, y_train)
print(small.score(X_train, y_train), small.score(X_val, y_val)) # e.g. 0.84 vs 0.80

The unlimited tree memorizes every row. Limiting depth lowers the training score and raises the validation score, which is the number that counts.

What goes wrong

  • Chasing training score. Making the model bigger until training accuracy reaches 100 percent is memorizing.
  • Adding data for an underfit model. If both scores are low and flat, more rows of the same features change little.
  • Tuning on the test set to close the gap. That hides the problem and leaks.
  • Blaming the model for a data gap. A model that fails only on a customer segment missing from training is not overfitting; it has never seen that segment.

Examples

Reading three runs

Run Train accuracy Validation accuracy Reading
Depth 2 tree 0.68 0.67 Underfit: both low. Allow more depth or add features.
Depth 6 tree 0.88 0.84 Good fit: high and close.
Unlimited tree 1.00 0.71 Overfit: memorized the training rows. Cap depth or prune.

Regularization strength

A logistic regression with C=1000 fits almost without penalty and scores 0.99 on training but 0.80 on validation. Dropping to C=0.1 gives 0.88 and 0.85. The smaller C shrinks the weights, so one noisy feature cannot dominate.

Key points

  • Compare training and validation scores together: a large gap means overfitting, two low scores mean underfitting.
  • Overfitting is treated with more data, simpler models, fewer features or stronger regularization.
  • Underfitting is treated with better features, a more flexible model or weaker regularization.
  • A learning curve shows whether more data would help or whether the model's capacity is the limit.
  • Change one thing at a time and re-read both scores after each change.

Common mistakes

  • Judging a model by its training score alone.
  • Adding more rows to fix underfitting when both scores are low and flat.
  • Making the model more complex to close a gap that is actually overfitting.
  • Tuning against the test set to shrink the train-test gap.

Check yourself · 2 of 3

Training accuracy is 0.62 and validation accuracy is 0.61 for a decision tree limited to depth 1. What is a sensible next step?

Choose one answer

Check yourself · 3 of 3

In scikit-learn, LogisticRegression(C=0.01) overfits less than C=100. Why?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic