Concepts / Measuring Model Accuracy

Measuring Model Accuracy

When you build a model, the question that matters most is how well it will perform on data it has never seen.

  • Programming

Why One Accuracy Number Can Mislead

When you build a model, the question that matters most is how well it will perform on data it has never seen. A model that scores well on the data it was built from may still fail on new cases, so the training data alone cannot answer the question. You need a way to measure performance on held-out data, meaning rows the model did not learn from.

A common first step is to divide the data into two parts. One part is the training set, used to build the model. The other is the test set, used to measure its accuracy. This approach is sound in principle, but it hides a weakness: the measured accuracy depends on which rows happened to land in the test set. Keep that weakness in mind as you work through this article, because it is the problem cross-validation was designed to solve.

Training Rows and Test Rows

Definition

A training set is the set of rows used to build the model. A test set is the set of rows held back and used only to measure how accurate the built model is. The two sets do not share rows, so the accuracy measurement reflects data the model did not see during building.

The separation is what makes the accuracy number meaningful. If the test rows were also used for building the model, the score would describe how well the model memorized them rather than how it would behave on new data. Whichever method you use, the rule stays the same: the rows that measure accuracy must stay out of the building step for that model.

The Single-Split Problem in Practice

A different random split could produce a noticeably different accuracy number. Suppose two people split the same dataset at random. Each person's test set contains different rows, so each person's model is measured on a different sample of the data. One split might happen to include mostly easy cases and report a high accuracy. Another might include mostly difficult cases and report a low one. A single split gives you one estimate, and that estimate may be lucky or unlucky.

The diagram shows the contrast. A single split leaves you with one number and no way to see how much it would move under a different arrangement of rows. Repeating the evaluation over several splits produces several numbers, so the spread of results becomes visible. The key idea of cross-validation is that every row gets a turn in the test set, so no single arrangement of rows decides the final score.

FeatureSingle train-test splitk-fold cross-validation
Number of test setsOnek, one per fold
Accuracy values producedOneOne per split, so k in total
Rows that serve as test dataOnly the rows in one test setEvery row, once across the rounds
Stability of the estimateDepends on one random arrangementCovers the whole dataset across several splits

How a single split compares with k-fold cross-validation

Partitioning Data Into Folds

Definition

k-fold cross-validation is the most commonly used form of cross-validation. The number k is chosen by the user, and the source describes usual choices as 5 or 10. The first step is always the same: the data is partitioned into k parts of approximately equal size. Each part is called a fold.

In the simplest arrangement, the first fifth of the data forms the first fold, the second fifth forms the second fold, and so on. The diagram below uses a generated example of ten rows divided into five folds of two rows each. Real datasets rarely divide so neatly.

The word approximately matters. When the number of rows does not divide evenly by k, the folds cannot all be exactly the same size, so they are made as close as possible. For example, if eleven rows were split into five folds, four folds would hold two rows and one fold would hold three. The method does not change because of this uneven size. Each fold still serves as the test set exactly once.

The letter k controls the trade-off. A larger k means more folds, so each fold is smaller and more models are trained. A smaller k means fewer, larger folds. The procedure itself, which is partition, rotate the test fold, train, score, and collect the results, stays the same for every value of k.

Rotating the Test Fold

Once the folds exist, a sequence of models is trained, and each one follows the same pattern. One fold is set aside as the test set. The model is built using the data in all the other folds. Its accuracy is then evaluated on the held-out fold, and that accuracy is recorded. The next model uses a different fold as its test set, and the process repeats until every fold has been the test set exactly once.

What do you think happens?

In five-fold cross-validation, how many models are trained, and how many accuracy values are collected?

  • One model and one accuracy value

  • Five models and five accuracy values

  • Five models and one accuracy value

  • Ten models and five accuracy values

Reveal answer
Answer:

Five models and five accuracy values

Each fold takes a turn as the test set, so there is one trained model and one recorded accuracy value per fold.

Read the diagram row by row. In each round, exactly one fold is the test set and the other four folds together form the training set. The test fold changes from round to round, so each row is held out exactly once. The trained model in each round is evaluated only on its own held-out fold, which means no round scores a model on rows that model was built from.

Counting Models and Accuracy Values

Worked example

Counting models and accuracy values for five-fold cross-validation

A dataset of ten rows is evaluated with five-fold cross-validation. How many models are trained, which rows does each model train on, and how many accuracy values are collected?

  1. 1
    Partition the data

    The ten rows are divided into five folds of approximately equal size. With ten rows, each fold holds two rows.

  2. 2
    Rotate the test fold

    Each of the five folds takes one turn as the test set, so there are five rounds in total.

  3. 3
    Count the models

    Each round builds one model, so five models are trained.

  4. 4
    Train each model on the other folds

    In each round the model is built from the remaining four folds, which is eight of the ten rows. Its two test rows are not used for building it.

  5. 5
    Count the accuracy values

    Each model is evaluated once on its held-out fold and that accuracy is recorded, so five accuracy values are collected.

Result

Five models are trained, each on eight rows, and five accuracy values are collected, one per fold.

Mistakes About Cross-Validation

  • Common mistake

    Thinking the test fold is also used for training in the same round

    What it looks like

    In round 1 of five-fold cross-validation, someone builds the model on all ten rows and then reports accuracy on fold 1.

    Why it fails

    The held-out fold is set aside for that round. The model is built from the other folds, so using fold 1 for building would contaminate the measurement.

    Do this instead

    In each round, build the model only on the training folds and evaluate it only on the one test fold.

  • Common mistake

    Believing that k must always be 5

    What it looks like

    A learner assumes that any cross-validation run uses five folds and never considers a different choice.

    Why it fails

    The number k is chosen by the user, and the source describes 5 or 10 as usual choices rather than fixed values.

    Do this instead

    Choose k deliberately. A larger k gives more folds and more models, and a smaller k gives fewer, larger folds.

  • Common mistake

    Expecting every fold to be exactly the same size

    What it looks like

    Eleven rows are divided into five folds and someone insists each fold must hold 2.2 rows.

    Why it fails

    Rows cannot be split fractionally. When the rows do not divide evenly, the folds are made as close to equal as possible.

    Do this instead

    Accept folds that differ by a row or so, and treat approximately equal as the rule.

  • Common mistake

    Assuming one split is enough because it looks like a complete evaluation

BeliefWhat the method actually doesWhy the correction matters
Cross-validation produces one final modelThe source describes training a sequence of models, one per round, and collecting an accuracy value from eachThe result is a set of accuracy values that together estimate generalization performance, not a single model verdict
The test fold is used for training in the same roundEach round holds out one fold and trains only on the remaining foldsTraining on the test fold would inflate the accuracy and defeat the purpose of held-out measurement
One random split gives a reliable scoreA single split depends on which rows landed in the test setRepeating the evaluation across folds reveals the score's variation instead of hiding it
Every fold is identical in sizeFolds are of approximately equal size, as close as possibleUneven folds are normal when the row count does not divide by k

Common misunderstandings about cross-validation and their corrections

Trying a Different Number of Folds

EASY

A dataset has 20 rows and you run ten-fold cross-validation. How many folds are there, how many rows does each fold hold, how many models are trained, and how many accuracy values are collected? Explain how each answer follows from the partitioning and rotation steps.

Hints
  • Start with the number of folds, which is k.
  • Divide the 20 rows by k to find the approximate size of each fold.
  • Each fold takes one turn as the test set, so count the rounds.

Key Takeaways

  • A single train-test split gives one accuracy number that depends on which rows landed in the test set, so it may be lucky or unlucky.
  • k-fold cross-validation partitions the data into k folds of approximately equal size, usually with k of 5 or 10 chosen by the user.
  • Each fold takes one turn as the test set while the remaining folds form the training set, so five-fold cross-validation trains five models and collects five accuracy values.
  • The test fold must never be used for building the model in its own round, and cross-validation does not describe a single final model being chosen from the rounds.

Related concepts

More concepts starting with M