Training accuracy alone says little. A model can memorise the training data or be too simple to learn it; both fail on new data.

Underfitting

A model underfits when it is too simple to capture the pattern, such as a straight line fitted to curved data. It has a high error on both the training and test sets. The remedy is a more flexible model, more useful features or less regularisation. Underfitting corresponds to high bias.

Overfitting

A model overfits when it learns the noise and accidents of the training data as if they were real patterns. It shows a very low training error but a much higher test error. A decision tree grown until every leaf is pure, or a 1-nearest-neighbour model, scores 100 percent on training data yet generalises poorly. Overfitting corresponds to high variance. Remedies include more data, a simpler model, pruning, early stopping, regularisation and fewer features.

The Bias and Variance Trade-off

As model complexity increases, bias falls and variance rises. The best model lies in between, where validation error is lowest. The practical signal is the gap: training score well above validation score means overfitting; both scores low means underfitting.

k-Fold Cross-Validation

A single validation split can be unlucky. In k-fold cross-validation the data is divided into k equal folds. The model is trained k times; each time one fold is the validation set and the other k − 1 folds are the training set. The k scores are averaged. This uses all data for both training and validation, gives a more stable estimate and is the standard method for small datasets. Typical values are k = 5 or 10. Leave-one-out is the extreme case where k equals the number of instances.

Definition: Overfitting. A condition in which a model fits the training data, including its noise, so closely that it performs much worse on new data.

Table 2.2: Underfitting, good fit and overfitting

Aspect Underfitting Good fit Overfitting
Training error High Low Very low
Test error High Low High
Model complexity Too low Suitable Too high
Problem High bias Balanced High variance

Example 2.2: Folds in 5-fold cross-validation

Problem: A dataset has 10 instances. List the validation indices of each fold of 5-fold cross-validation (no shuffling), and give the training size.

Solution:

  1. With k = 5 each fold has 10 / 5 = 2 instances.
  2. The validation folds are {0, 1}, {2, 3}, {4, 5}, {6, 7} and {8, 9}.
  3. In each round the other four folds train the model: 8 instances.
  4. After five rounds every instance has been validated exactly once; the final score is the mean of the five scores.

Result: Five folds of 2 validation instances; training size 8 each time.

from sklearn.datasets import make_moons
from sklearn.model_selection import train_test_split, KFold
from sklearn.tree import DecisionTreeClassifier
for tr, te in KFold(5).split(range(10)):
    print("validation fold:", [int(i) for i in te])
X, y = make_moons(n_samples=200, noise=0.35, random_state=1)
Xa, Xb, ya, yb = train_test_split(X, y, test_size=0.3,
                                  random_state=1)
for d in (1, 3, None):
    t = DecisionTreeClassifier(max_depth=d, random_state=0)
    t.fit(Xa, ya)
    print("depth", d, "train", round(t.score(Xa, ya), 3),
          "test", round(t.score(Xb, yb), 3))

Output

validation fold: [0, 1]
validation fold: [2, 3]
validation fold: [4, 5]
validation fold: [6, 7]
validation fold: [8, 9]
depth 1 train 0.836 test 0.8
depth 3 train 0.9 test 0.883
depth None train 1.0 test 0.833

Common mistakes

  • Judging a model by training accuracy alone.
  • Adding complexity until training error is zero and calling it the best model.
  • Reporting the best of many cross-validation folds instead of the mean.
  • Ignoring the gap between training and validation scores.

Key takeaways

  • Underfitting: poor on train and test (high bias).
  • Overfitting: great on train, poor on test (high variance).
  • k-fold cross-validation trains k times and averages the k scores.
  • A large train-test gap signals overfitting.

Practice questions

  1. Which error is very low in overfitting?
  2. How many models does 10-fold cross-validation train?
  3. Give one remedy for overfitting.

Answers to the practice questions

  1. The training error.
  2. Ten.
  3. More data, pruning, regularisation or a simpler model.

Sources

  • Official BCA syllabus Introduction to Machine Learning, course code 26BCA404DSE21, Unit II