When a model is not good enough, there are two common fixes: get more data, or change how complex the model is. Learning curves tell you whether more data would help. Validation curves tell you how one setting, such as tree depth, moves the model between underfitting and overfitting. Both compare training scores with cross-validated scores.
In this lesson you will learn
- What a learning curve shows and how to compute one
- Reading the gap between training and validation scores
- Using
validation_curveto pick a setting - Diagnosing high bias and high variance
You should know overfitting and cross-validation.
Learning Curve: Would More Data Help?
A learning curve trains the same model on growing slices of the data and records two scores each time: on the slice it trained on, and on held-out folds.
import numpy as np
from sklearn.datasets import load_digits
from sklearn.model_selection import learning_curve
from sklearn.svm import SVC
X, y = load_digits(return_X_y=True)
sizes, train_scores, val_scores = learning_curve(
SVC(gamma=0.001), X, y, cv=5, train_sizes=[0.1, 0.25, 0.5, 0.75, 1.0])
print("train size train acc validation acc")
for n, tr, va in zip(sizes, train_scores.mean(axis=1), val_scores.mean(axis=1)):
print(f"{n:>10} {tr:>9.3f} {va:>14.3f}")
Output
train size train acc validation acc
143 0.994 0.818
359 0.998 0.896
718 0.999 0.947
1077 0.999 0.962
1437 0.999 0.972
train_sizesare fractions of each training fold. With 5 folds the full fold has 1,437 of the 1,797 digit images, so 10% is 143 images.- The training score is almost perfect at every size. The SVM can memorise the images it sees.
- The validation score climbs steadily from 81.8% to 97.2%. The gap between the two lines shrinks as data grows.
- The curve is still rising at the end, so more images would probably help a little more.
Validation Curve: Tuning One Setting
A validation curve keeps the data fixed and changes one hyperparameter. Here it is the depth of a decision tree.
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, 6, 8, 12]
train_scores, val_scores = validation_curve(
DecisionTreeClassifier(random_state=0), X, y,
param_name="max_depth", param_range=depths, cv=5)
print("max_depth train acc validation acc gap")
for d, tr, va in zip(depths, train_scores.mean(axis=1), val_scores.mean(axis=1)):
print(f"{d:>9} {tr:>9.3f} {va:>14.3f} {tr - va:.3f}")
Output
max_depth train acc validation acc gap
1 0.924 0.900 0.025
2 0.952 0.928 0.024
3 0.974 0.917 0.057
4 0.985 0.916 0.069
6 0.996 0.914 0.083
8 1.000 0.916 0.084
12 1.000 0.917 0.083
- At depth 1 the tree is a single question. Both scores are lower and close together, which is the sign of underfitting.
- Validation accuracy peaks at depth 2 with 92.8%.
- From depth 8 the tree scores 100% on its training data, yet validation stays around 91.6%. The growing gap is the sign of overfitting.
param_nameuses the parameter’s name on the model. In a pipeline, add the step name, as in"decisiontreeclassifier__max_depth".
Reading the Curves
| What you see | Diagnosis | What usually helps |
|---|---|---|
| Both scores low and close together | High bias (underfitting) | More complex model, better features |
| Training high, validation much lower | High variance (overfitting) | More data, simpler model, regularization |
| Validation still rising at the largest size | Data-limited | Collect more data |
| Validation flat over the last sizes | Data is not the bottleneck | Change the model or features instead |
Common Mistakes
- Looking only at the validation line. The gap to the training line is what tells you bias from variance.
- Using too few folds, so the lines bounce around and hide the pattern.
- Tuning by validation curve, then reporting that same score as the final result. Keep a separate test set.
- Forgetting the cost. Each point trains the model once per fold, so large grids can be slow.
Practice Exercises
Exercise 1. Draw a validation curve for n_neighbors in KNN on the breast cancer data with values 1, 3, 5, 9, 15, 25, 50.
Show answer
from sklearn.neighbors import KNeighborsClassifier
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
pipe = make_pipeline(StandardScaler(), KNeighborsClassifier())
ks = [1, 3, 5, 9, 15, 25, 50]
tr, va = validation_curve(pipe, X, y, param_name="kneighborsclassifier__n_neighbors",
param_range=ks, cv=5)
for k, a, b in zip(ks, tr.mean(axis=1), va.mean(axis=1)):
print(k, round(a, 3), round(b, 3))
At k = 1 the training score is 1.0, which is overfitting. Larger k smooths the model; the validation score stays high over a wide middle range.
Exercise 2. A learning curve ends with training 0.70 and validation 0.69. Will collecting more data fix it?
Show answer
Probably not. Both scores are low and almost equal, which is underfitting. The model is too simple for the task, so try a more flexible model or better features.
Frequently Asked Questions
How is a validation curve different from grid search?
A validation curve changes one setting and shows training and validation scores side by side, which helps you understand the model. Grid search tries combinations of many settings and just returns the best one.
How do I plot these curves?
Use LearningCurveDisplay.from_estimator and ValidationCurveDisplay.from_estimator from sklearn.model_selection, or plot the mean arrays yourself with Matplotlib.
Summary
- A learning curve varies the amount of training data; a validation curve varies one setting.
- Low and close scores mean underfitting; a large gap means overfitting.
- A validation score still rising at the end means more data should help.
