A model that memorises fails on new data.

Lesson 3 of 7About 10 minutes5 steps

Every model is judged on data it hasn’t seen. Learn on one part, test on another, and watch for the point where a more flexible model starts learning the noise instead of the pattern.

  • Explain why a test set is held back
  • Spot overfitting and underfitting from two numbers
  • Say what cross-validation and regularisation are for

You need: Lesson 1.

  1. 1Distributions and sampling
  2. 2Confidence intervals and tests
  3. 3Training, testing and overfitting
  4. 4Judging a classifier
  5. 5Trees and ensembles
  6. 6Clustering and PCA
  7. 7How a neural network learns
0 of 5 steps
  1. Hold some data back

    Predict first

    Why not judge a model on the data it learned from?

    Just show me

    Because a model can memorise: a decision tree with no depth limit scores 1.00 on the 4,000 churn customers it learned from and 0.52 on 1,000 new ones, barely better than a coin.

    The score here is AUC: the chance the model ranks a customer who left above one who stayed, where 0.5 is a coin toss and 1 is perfect (lesson 4 explains it). The ML guide keeps 1,000 of its 5,000 customers aside until the end; their score estimates how the model will do on customers it has never met, the only score that matters.

    Quick check

    You try 50 settings and keep the one with the best test score. The problem?

    Choosing by the test set leaks it into training; choose with a validation set or cross-validation.

  2. Too stiff, too flexible

    Predict first

    16 points from a wavy curve, plus noise. Degree 3 or degree 15: which predicts new points better?

    Just show me

    Degree 3: an error of 0.31 on 200 new points, against 3,280 for degree 15.

    Degree 15 bends through all 16 points it learned from (an error of 0 there) and swings wildly between them: it learned the noise. That’s overfitting. Degrees 0 to 2 are too stiff to follow the curve, missing old and new points alike: underfitting. Degree 3 misses the 16 points by a little (0.24) and new ones by about as much, close to the noise itself (0.3).

    The overfitting playground’s 16 points and 200 new ones, degrees 0 to 10 (degree 15’s 3,280 is far off the chart)
    DegreeError on the 16 points it learned fromError on 200 new points
    00.550.66
    10.520.59
    20.480.62
    30.240.31
    40.210.34
    50.210.37
    60.210.37
    70.190.41
    80.190.52
    90.180.86
    100.180.89

    The overfitting playground’s 16 points and 200 new ones, degrees 0 to 10 (degree 15’s 3,280 is far off the chart)

    Fit the curve yourself in the overfitting playground

    Quick check

    Training error 0.05, test error 0.40. What’s happening?

    A big gap between training and test error is the signature of overfitting.

  3. The same story in a real model

    Predict first

    Decision trees on the churn table, deeper and deeper. Which depth scores best on the test customers?

    Just show me

    Depth 3: 0.72 on the test customers. Depth 8 scores 0.85 on the training customers but 0.63 on the test ones; no limit, 1.00 and 0.52.

    Each level lets the tree split customers into smaller groups, until with no limit it has 641 leaves, many holding a single customer. The training score climbs all the way; the test score peaks early and falls. Limiting the depth, or asking for a minimum number of customers per leaf, is how trees are regularised.

    Decision trees on the ML guide’s churn table, scikit-learn 1.8.0
    DepthAUC on the 4,000 training customersAUC on the 1,000 test customers
    10.640.62
    20.700.69
    30.730.72
    50.780.66
    80.850.63
    No limit1.000.52

    Decision trees on the ML guide’s churn table, scikit-learn 1.8.0

    Quick check

    How do you choose the depth fairly?

    Cross-validation chooses settings without touching the test set.

  4. Cross-validation

    Predict first

    5-fold cross-validation trains how many models?

    Just show me

    5: each fold takes a turn as the part held out while the other four train.

    Cross-validation uses all the training data for learning and for checking: split it into k folds, train k times, score each time on the fold left out, and average. Stratified folds keep the churn rate the same in each fold, which matters when only 6.9% churn, as here. For data in time order, validate only on later periods.

    Quick check

    Why stratified folds when 7% churn?

    A fold with very few churners would give a noisy, unfair score.

  5. More data, or a penalty

    Predict first

    Degree 15 again, now learned from 100 points instead of 16. Its error on new points?

    Just show me

    0.37: more data tames a flexible model. A ridge penalty on the 16 points gets it to 0.40.

    The two cures for overfitting: more data, and regularisation, a penalty for complexity. Ridge (L2) shrinks every coefficient; lasso (L1) can set some exactly to zero, dropping features. For trees it’s depth and leaf size; for networks, dropout and weight decay. Underfitting needs the opposite: a more flexible model or better features.

    Quick check

    Your model underfits. Which helps?

    Underfitting means too simple: add capacity or information.

In the interview: training, testing and overfitting.

Ask Prism about this lesson

Sources

  1. scikit-learn, “Cross-validation: evaluating estimator performance”.
  2. Hastie, Tibshirani and Friedman, “The Elements of Statistical Learning”, 2nd edition, 2009, chapter 7: model assessment and selection.
  3. Our overfitting playground, its own code, points and seeds.

Back: lesson 2, Confidence intervals and tests