Sub topic 2
Computing a Learning Curve
Assume you know supervised learning, scikit-learn estimators, NumPy, and the basic meaning of overfitting. A learning curve shows how training and validation scores change as the training set grows.
Computing a learning curve
learning_curve trains the estimator on several training-set sizes. For each size, cross-validation produces scores for both the training portion and the held-out validation portion.
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import load_digits
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import learning_curve
X, y = load_digits(return_X_y=True)
sizes, train_scores, val_scores = learning_curve(
LogisticRegression(max_iter=2000),
X,
y,
train_sizes=np.linspace(0.1, 1.0, 5),
cv=5,
scoring="accuracy",
)
train_mean = train_scores.mean(axis=1)
train_std = train_scores.std(axis=1)
val_mean = val_scores.mean(axis=1)
val_std = val_scores.std(axis=1)
fig, ax = plt.subplots()
ax.plot(sizes, train_mean, marker="o", label="Training")
ax.fill_between(sizes, train_mean - train_std, train_mean + train_std, alpha=0.2)
ax.plot(sizes, val_mean, marker="o", label="Validation")
ax.fill_between(sizes, val_mean - val_std, val_mean + val_std, alpha=0.2)
ax.set_xlabel("Training samples")
ax.set_ylabel("Accuracy")
ax.legend()
plt.show()The score arrays have shape (number_of_sizes, number_of_folds). Averaging across axis=1 gives one mean score for each size.
Reading a learning curve
A large training score with a much lower validation score indicates high variance, or overfitting. More data may reduce this gap. If both scores are low and close together, the model has high bias, or underfitting. A more flexible model or better features may help. If the curves meet at a good score, collecting more data is unlikely to help much.
Computing a validation curve
A validation curve varies one hyperparameter while keeping the others fixed. This example tests gamma, which controls the influence range of an RBF support vector machine.
import numpy as np
from sklearn.datasets import load_digits
from sklearn.model_selection import validation_curve
from sklearn.svm import SVC
X, y = load_digits(return_X_y=True)
gamma_values = np.logspace(-6, -1, 6)
train_scores, val_scores = validation_curve(
SVC(kernel="rbf"),
X,
y,
param_name="gamma",
param_range=gamma_values,
cv=5,
scoring="accuracy",
)
for gamma, train, validation in zip(
gamma_values, train_scores.mean(axis=1), val_scores.mean(axis=1)
):
print(f"gamma={gamma:.0e} train={train:.3f} validation={validation:.3f}")Reading a validation curve
Choose a value near the highest validation score, preferably from a broad stable region rather than a sharp peak. A high training score with a lower validation score suggests excessive flexibility. Low scores for both curves suggest too little flexibility. Because this choice uses validation data, reserve a separate test set for final evaluation.
Exercise
If training accuracy keeps rising but validation accuracy falls as a hyperparameter increases, what is happening?
Show answer
The model is becoming too flexible and overfitting. Choose a smaller value or add regularization.