Diagnosing Overfitting with Validation Curves vs Learning Curves in scikit-learn
Use validation_curve to spot over- or underfitting from a single hyperparameter; use learning_curve to judge if more data will reduce variance. This article explains how to interpret the train-validation gap and avoid common pitfalls.
On this page
Key idea
To diagnose overfitting by the gap between training and validation error, first identify which lever you might change. If the suspected cause is model complexity, use validation_curve: it varies one hyperparameter (e.g., C, depth, number of features) and plots training and validation scores. A large gap at high complexity means overfitting; both low means underfitting. If the question is whether collecting more data will close the gap, use learning_curve: it varies training set size and shows whether the validation score improves with more samples. Always keep preprocessing inside a Pipeline to prevent leakage, and remember that selecting a hyperparameter from a validation curve biases the validation score, so use a separate test set for final generalization estimates.
Choose the curve based on the diagnostic question
Validation curves and learning curves answer different questions. The scikit-learn documentation says: 'If the training score and the validation score are both low, the estimator will be underfitting. If the training score is high and the validation score is low, the estimator is overfitting.' The validation_curve varies a single hyperparameter, so it directly shows how complexity affects the gap. The learning_curve varies the number of training samples, showing whether more data reduces variance. Decide first: are you about to tweak a hyperparameter (use validation_curve) or decide if more data is worth acquiring (use learning_curve)?
Interpret the gap between training and validation scores on the validation curve
On a validation curve, the horizontal axis is the hyperparameter value. If both scores are low and close, the model is underfitting (high bias). If the training score is high and the validation score drops as the hyperparameter increases, overfitting (high variance) is present. The gap between the two curves is the sensitivity of the model to that hyperparameter. A narrow range where both scores are high and the gap is small indicates a good operating point. The scikit-learn docs state that a low training score with a high validation score is usually not possible, so the only two problematic patterns are both-low and train-high-with-valid-low.
Use validation_curve when tuning a single hyperparameter, learning_curve when assessing data size
The trade-off is clear: validation_curve reveals which hyperparameter value balances bias and variance, but it does not tell you whether adding more data will help. learning_curve does the opposite: it shows how the validation score improves with more samples, but it does not indicate which hyperparameter to adjust. In practice, run validation_curve first to pick a reasonable hyperparameter, then run learning_curve with that value to see if the remaining gap closes with more data. The source notes that 'adding more training samples will most likely increase generalization' when the training score is much higher than the validation score for small training sizes.
Account for validation bias and cross-validation overhead
Both curves use cross-validation, which is computationally expensive. validation_curve with many hyperparameter values multiplies the cost by the number of folds. Also, the scikit-learn docs warn: 'if we optimize the hyperparameters based on a validation score the validation score is biased and not a good estimate of the generalization any longer.' Therefore, after using a validation curve to pick a hyperparameter, evaluate the final model on a separate hold-out test set. Additionally, all preprocessing must be inside a Pipeline to avoid data leakage; the documentation stresses that transformations should be learned only on the training fold. Fix random_state for splitters and consider passing a RandomState instance to the estimator for robust comparisons, but use an integer splitter for consistent folds across calls.
import numpy as np
from sklearn.datasets import load_iris
from sklearn.model_selection import validation_curve, learning_curve
from sklearn.svm import SVC
from sklearn.utils import shuffle
X, y = load_iris(return_X_y=True)
X, y = shuffle(X, y, random_state=0)
# validation_curve: vary C, see over/underfitting
param_range = np.logspace(-7, 3, 5)
train_scores, valid_scores = validation_curve(
SVC(kernel='linear'), X, y, param_name='C', param_range=param_range, cv=5)
# learning_curve: see if more data helps for a chosen C
chosen_C = 1.0 # reasonable from validation curve
train_sizes, train_scores_lc, valid_scores_lc = learning_curve(
SVC(kernel='linear', C=chosen_C), X, y, train_sizes=[50, 80, 110], cv=5)
print('Validation curve mean train scores:', train_scores.mean(axis=1))
print('Validation curve mean valid scores:', valid_scores.mean(axis=1))
print('Learning curve mean valid scores:', valid_scores_lc.mean(axis=1))Applicability
- Are you trying to tune one hyperparameter rather than evaluate the benefit of more data?
- Is your scoring metric appropriate for the business cost of errors?
- Are all preprocessing steps inside a Pipeline fitted only on training folds?
- Do you have a separate test set to estimate generalization after hyperparameter selection?
- Is the dataset size and number of hyperparameter values small enough to avoid excessive cross-validation cost?
Where this applies
Validation_curve only examines a single hyperparameter; for multiple parameters use grid search or randomized search. Interpretation assumes a proper train-validation split without leakage; all preprocessing must be in a pipeline. Both curves can be misleading if the dataset is very small or the scoring metric does not reflect actual error costs. Cross-validation randomness must be controlled with fixed random_state and consistent folds when comparing curves.