Concepts / Model Complexity and Accuracy

Model Complexity and Accuracy

Generalization is the ability of a model to make accurate predictions on new, unseen data that resembles the training set.

  • Programming

A Perfect Score That Misleads

Imagine a model that gets every single training example right. It feels like a success, and it is tempting to call the job finished. In supervised learning that feeling is misleading. A model exists to make predictions about cases it has never seen, and a perfect score on the cases it already studied says little about that goal.

The belief behind the mistake is easy to understand. We normally build a model so that it predicts the training data accurately, and we hope that accuracy carries over to new data. Often that hope is justified when the training and test sets have enough in common. The problem is that the training score alone cannot tell us whether the hope is justified in a particular case.

To see how this plays out, consider a made-up scenario. A novice data scientist wants to predict whether a customer will buy a boat, using records of past boat buyers and customers known not to be interested. The goal is to send promotional emails only to people who are likely to purchase. The records the scientist has available cover 12 people whose answers are already known.

What do you think happens?

Suppose a model simply stores each training customer's age together with the answer, and when it is asked about an age it does not recognize, it says no. What would this model score on the training data?

  • A perfect score, because every training answer is stored

  • A low score, because it learned no rule

  • About half, because the answers are split between yes and no

Reveal answer
Answer:

A perfect score, because every training answer is stored

The memorizer reproduces each answer it was shown, so its training score is perfect. Its weakness only appears on ages it has not seen.

Tracing the Memorizer Step by Step

Work through the memorizer one request at a time. Its behavior has two cases: an age it has stored, and an age it has never seen. The trace below shows that the training score and the answer for a new customer come from completely different sources.

Worked example

What the memorizer does with a new customer

The memorizer has stored the age and answer of every training customer. A new customer aged 60 arrives, and the model must say whether that customer will buy a boat.

  1. 1
    Store the training pairs

    For each training customer, the model keeps the pair of age and answer. Nothing is summarized and no pattern is extracted.

  2. 2
    Score the training data

    Each training customer's age is looked up and returns that customer's own stored answer. Every answer matches, so the training score is perfect.

  3. 3
    Meet an unrecognized age

    Age 60 is not among the stored ages, so the lookup finds nothing to reproduce.

  4. 4
    Fall back to the default

    The model uses its default answer of no. That answer comes from a default setting, not from any pattern in the data.

Result

The model is perfect on the training data but has learned nothing that helps with a customer it has not seen. Its answer for age 60 is a default, so the training score says nothing about its performance on new customers.

Training Score Versus Generalization

Definition

Generalization is the ability of a model to make accurate predictions on new, unseen data that resembles the training set.

A training score measures how well a model reproduces answers it was shown. Generalization measures how well it predicts answers it was not shown. These two quantities are different, and they can differ enormously. The memorizer shows the gap at its most extreme: a perfect training score alongside no real ability to predict new customers.

Training examplesanswers already knownLearned modelbuilt from the examplesPredictions on newdatacustomers never seenAccuracy on new datathe test of generalizationfitapplied tocompared with answers
What is the sequence of steps from training data to a learned model to predictions on new data, and where is generalization actually tested?

The diagram shows where generalization is actually tested. Training examples are used to fit the model, but the model is judged on predictions for new data, which are compared with answers the model never used during fitting. A high score in the first stage tells you only that fitting worked.

Why a Rule Can Be Perfect on the Table

After studying the table, the novice scientist proposed this rule: if the customer is older than 45 and has fewer than 3 children or is not divorced, then they want to buy a boat. Asked how well it works, the scientist said it is 100 percent accurate. On the table itself, that claim is true. But the scientist's real aim was never to describe these 12 people, whose answers are already known. The aim is to predict new customers, and a rule that is perfect on the known table may fail on people outside it.

There is also a way to fit the table perfectly without finding any real rule. Because no age appears twice in the data, one could list the ages of the boat buyers and declare that exactly those people want a boat. That rule is also perfectly accurate on the table, but it is memorization with a different label. Both the novice's rule and this lookup are warnings: many rules can explain the training data perfectly, and perfect explanation of the training data is not the same as a rule that works on new customers.

Memorized answerseach training age mapped toits answerLearned patterna rule that applies beyondthe stored agesTraining score 100percentevery stored answerreproducedAccuracy on newcustomersthe measure that matters
How does a very complex model that fits every training point differ in shape from a simpler model that captures the real pattern, and why does the complex one fail on new data?

The before side of the comparison is the memorizer's structure: a lookup of stored pairs that scores perfectly on what it has seen. The after side describes a model whose rule can answer for cases outside the stored list. The source's point is that the training score cannot tell these two apart, so the score must be paired with a check on examples the model did not study.

Model Complexity as a Dial

Model complexity is the lever that connects these ideas. If we allow ourselves to build very complex models, we can always be as accurate as we like on the training set. A model that is too complex fits the training data too closely and may fail to generalize. This situation is called overfitting. The novice's rule is an example: its clause 'or is not divorced' depends on a single customer, so it is unlikely to hold up for new customers.

The opposite failure is underfitting. Here the model is too simple to capture the underlying patterns in the training data. It does not fit the training data well, and so it also does not generalize well. Overfitting and underfitting sit at opposite ends of a complexity range, and a model that generalizes well lies somewhere between them.

Too simplecannot capture trainingpatternsToo complexfits training data verycloselyPoor training fitdoes not fit training datawellVery close trainingfitmay look perfect ontraining dataPoor new-data resultsdoes not generalizePoor new-data resultsfails to generalize
Given a model's complexity and its performance on training and new data, which of the two failure modes is it in?

Read the two columns against each other. Both failure modes produce poor predictions on new data, but they arise for different reasons and look different on the training data. An underfit model is weak on the training data as well as on new data. An overfit model can look excellent on the training data and still fail on new data, which is why the training score is the misleading signal.

Diagnosing Overfitting and Underfitting

The table below sets the two failure modes side by side so that a model can be checked against each row. Use the complexity of the model together with its behavior on training data and on new data.

Failure modeModel complexityTraining data behaviorBehavior on new dataWhy it happens
OverfittingToo complexFits very closely, possibly perfectlyFails to generalizeThe model captures details of the training set that do not carry over
UnderfittingToo simpleDoes not fit wellDoes not generalize wellThe model cannot capture the underlying patterns in the training data
Generalizing modelBetween the two extremesFits the training data reasonablyPredicts new data accuratelyThe model captures the real pattern without fitting noise

Checking a model against complexity and new-data behavior

Mistakes Beginners Make

  • Common mistake

    Judging a model by its training accuracy alone

    What it looks like

    The novice's rule is reported as 100 percent accurate on the 12 known customers, and that figure is treated as proof that it will work.

    Why it fails

    Training accuracy measures how well the model reproduces answers it was shown. It does not measure how well the model predicts customers it has not seen.

    Do this instead

    Keep some data out of training and measure performance on those held-out examples.

  • Common mistake

    Assuming a complex model is always the better model

    What it looks like

    A rule with several clauses is preferred because it fits every training example.

    Why it fails

    A model that is too complex fits the training data too closely and may overfit. The source states that complexity can make training accuracy as high as we like without improving generalization.

    Do this instead

    Ask whether the rule is simpler than the data would justify, and check its behavior on new data.

  • Common mistake

    Trusting a rule that depends on a handful of records

    What it looks like

    The clause 'or is not divorced' is kept even though it depends on a single customer.

    Why it fails

    A rule that hinges on very little data is unlikely to hold up for new customers.

    Do this instead

    Check whether each part of the rule is supported by many examples, not just one or two.

  • Common mistake

    Confusing a lookup of training answers with learning a pattern

Checking a Model Before Trusting It

The practical habit this topic builds is to refuse to judge a model by its training score alone. Each check below follows directly from the definition of generalization.

  1. 1
    Keep some data out of training so that you can measure performance on examples the model has not seen.
  2. 2
    Compare the training result with the result on the held-out examples, and note whether a large gap appears.
  3. 3
    Look at whether the rule is simpler than the data would justify, since an overly complex rule points toward overfitting.
  4. 4
    Check whether the rule depends on a handful of records, since such a rule is unlikely to hold up for new customers.

Applying the Diagnosis

EASY

A model scores 100 percent on its training examples but performs poorly on examples it has never seen. Using only the definitions in this article, decide whether it is overfitting or underfitting, and explain which measurement led you to that answer.

Hints
  • Ask first how the model performed on the training data, then how it performed on new data.
  • Recall which failure mode the source describes as fitting the training data too closely.
MEDIUM

The novice's rule includes the clause 'or is not divorced'. Explain why this clause is a weak basis for predicting new customers, and name the check from this article that would expose the problem.

Hints
  • Think about how many customers support that clause.
  • The check is about whether a rule depends on a handful of records.

Key Takeaways in Brief

  1. 1
    Generalization is the ability of a model to make accurate predictions on new, unseen data that resembles the training set.
  2. 2
    A 100 percent training score does not guarantee good performance on new data, because training accuracy does not measure generalization.
  3. 3
    Overfitting happens when a model is too complex and fits the training data closely but fails on new data; underfitting happens when a model is too simple to capture the training patterns and also fails on new data.
  4. 4
    A generalizing model lies between these extremes, and its quality is judged on new data rather than on its training score.

At a glance

  • Generalization is the ability of a model to make accurate predictions on new, unseen data that resembles the training set.
  • A 100 percent training score does not guarantee good performance on new data, because training accuracy does not measure generalization.
  • Overfitting is a model that is too complex and fits the training data too closely; underfitting is a model that is too simple to capture the training patterns.
  • Both failures predict poorly on new data, and a model that generalizes well lies between them.

Related concepts

More concepts starting with M