TExcelTAD
Home › Machine Learning › Learning & Validation Curves

Learning & Validation Curves

When a model isn't good enough, what should you do next — collect more data, use a more complex model, or a simpler one? Two diagnostic plots answer that question with evidence instead of guesswork:

  • a validation curve shows the score as one hyperparameter varies,
  • a learning curve shows the score as the amount of training data grows.

Both compare training scores with cross-validated scores, which is how you see underfitting and overfitting.

Validation curve

validation_curve trains the model with each value of one hyperparameter using cross validation. Here we vary the max_depth of a decision tree on the breast-cancer data:

python · try it yourself
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import validation_curve
from sklearn.tree import DecisionTreeClassifier

X, y = load_breast_cancer(return_X_y=True)
depths = [1, 2, 3, 4, 5, 6, 8, 10]
train_scores, val_scores = validation_curve(
    DecisionTreeClassifier(random_state=0), X, y,
    param_name="max_depth", param_range=depths, cv=5)

train_mean, val_mean = train_scores.mean(axis=1), val_scores.mean(axis=1)
plt.plot(depths, train_mean, marker="o", label="training")
plt.plot(depths, val_mean, marker="o", label="cross-validation")
plt.xlabel("max_depth")
plt.ylabel("accuracy")
plt.legend()
plt.show()

for d, tr, va in zip(depths, train_mean, val_mean):
    print(f"depth {d:2}: train={tr:.3f}  cv={va:.3f}  gap={tr - va:.3f}")
depth  1: train=0.924  cv=0.900  gap=0.025
depth  2: train=0.952  cv=0.928  gap=0.024
depth  3: train=0.974  cv=0.917  gap=0.057
depth  4: train=0.985  cv=0.916  gap=0.069
depth  5: train=0.991  cv=0.916  gap=0.076
depth  6: train=0.996  cv=0.914  gap=0.083
depth  8: train=1.000  cv=0.916  gap=0.084
depth 10: train=1.000  cv=0.917  gap=0.083

How to read it:

  • On the left, both scores are lower and close together: the model is too simple (underfitting / high bias).
  • Further right, the training score climbs to 1.0 while the validation score stalls or drops — a widening gap means overfitting (high variance).
  • The best setting is where the validation score peaks.

Learning curve

learning_curve trains on growing subsets of the data. It tells you whether more data would help:

python · try it yourself
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import load_digits
from sklearn.model_selection import learning_curve
from sklearn.naive_bayes import GaussianNB
from sklearn.svm import SVC

X, y = load_digits(return_X_y=True)
sizes = np.linspace(0.1, 1.0, 5)

fig, axes = plt.subplots(1, 2, figsize=(10, 4), sharey=True)
for ax, (name, model) in zip(axes, [("Gaussian NB", GaussianNB()), ("SVC (rbf)", SVC(gamma=0.001))]):
    n, train_s, val_s = learning_curve(model, X, y, train_sizes=sizes, cv=5)
    ax.plot(n, train_s.mean(axis=1), marker="o", label="training")
    ax.plot(n, val_s.mean(axis=1), marker="o", label="cross-validation")
    ax.set_title(name)
    ax.set_xlabel("training samples")
    ax.legend()
    print(f"{name:12}", "cv:", val_s.mean(axis=1).round(3), " train:", train_s.mean(axis=1).round(3))
axes[0].set_ylabel("accuracy")
plt.show()
Gaussian NB  cv: [0.615 0.777 0.788 0.802 0.806]  train: [0.985 0.922 0.888 0.886 0.872]
SVC (rbf)    cv: [0.818 0.923 0.951 0.965 0.972]  train: [0.994 0.998 0.999 0.999 0.999]

Two typical shapes:

Shape Diagnosis What helps
Both curves low and close together, flattening early high bias (underfitting) a more flexible model, better features — more data won't help
Training high, validation lower but still rising high variance more data, regularization, simpler model

Gaussian Naive Bayes levels off around 0.80, and its training score falls towards the validation score as data grows: the model is too simple (its independence assumption limits it), so more digits won't help much. The SVC's validation score keeps improving as data grows, approaching its near-perfect training score.

Reading the numbers without a plot

Plots are easiest, but you can diagnose from the numbers too. A small helper:

python · try it yourself
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import cross_validate
from sklearn.tree import DecisionTreeClassifier

def diagnose(model, X, y):
    s = cross_validate(model, X, y, cv=5, return_train_score=True)
    tr, va = s["train_score"].mean(), s["test_score"].mean()
    if tr - va > 0.05:
        verdict = "overfitting: big train/validation gap"
    elif va < 0.92:
        verdict = "underfitting: both scores low"
    else:
        verdict = "looks reasonable"
    print(f"train={tr:.3f} cv={va:.3f} -> {verdict}")

X, y = load_breast_cancer(return_X_y=True)
diagnose(DecisionTreeClassifier(max_depth=1, random_state=0), X, y)
diagnose(DecisionTreeClassifier(max_depth=None, min_samples_leaf=1, random_state=0), X, y)
diagnose(DecisionTreeClassifier(max_depth=3, min_samples_leaf=5, random_state=0), X, y)
train=0.924 cv=0.900 -> underfitting: both scores low
train=1.000 cv=0.917 -> overfitting: big train/validation gap
train=0.966 cv=0.924 -> looks reasonable

The thresholds (0.05 gap, 0.92 score) are specific to this problem — what counts as "low" always depends on the task and the baseline.

Tip: Learning curves are an excellent argument in a project meeting. "The validation curve is still rising" justifies collecting more data; "both curves are flat and close" says the money is better spent on features or a different model.

Test yourself

A validation curve shows training accuracy 1.00 and validation accuracy 0.80 at high complexity. What's the diagnosis?

  1. Underfitting
  2. Overfitting
  3. Perfect fit
  4. Not enough features

Training and validation curves are both low, close together and flat as data grows. Will more data help?

  1. Yes, a lot
  2. Probably not; the model is too simple (high bias)
  3. Only if the data is shuffled
  4. Only for regression

What does a learning curve vary on its x-axis?

  1. A hyperparameter
  2. The number of training samples
  3. The random seed
  4. The threshold