Concepts / Evaluating Classification Models

Evaluating Classification Models

Evaluating a classification model involves calculating the classification accuracy.

  • Programming

Asking Whether a Trained Model Works

Once a classification model has been trained, the next question is whether it works. A classifier takes an input and assigns it to a category, such as spam or not spam, or a diagnosis label. Each assignment is either right or wrong, and the simplest way to summarize that behaviour is to count how often the model is right. Evaluating a classification model involves calculating the classification accuracy. That single number is the starting point for judging a model, and this article covers what it means, how to compute it, and what it cannot tell you.

What do you think happens?

A spam filter reads 10 emails. It labels 8 of them correctly and 2 of them incorrectly. What fraction of its labels were correct?

  • 0.2

  • 0.5

  • 0.8

  • 8

Reveal answer
Answer:

0.8

Eight correct labels out of ten total predictions gives 8 divided by 10, which is 0.8. As a percentage this is 80 percent.

Lining Up Predictions With True Labels

To measure accuracy you need two lists of the same length. The first list holds the model's predictions. The second holds the true labels, which are the correct answers for the same inputs. Accuracy works by pairing the two lists position by position: the prediction at position 0 is compared with the true label at position 0, the prediction at position 1 with the true label at position 1, and so on. Each pair is either a match, which counts as correct, or a mismatch, which counts as an error.

Email 1true: spam | predicted:spamEmail 2true: not spam | predicted:not spamEmail 3true: spam | predicted: notspamEmail 4true: spam | predicted:spamEmail 5true: not spam | predicted:not spam
How do I line up each prediction with its true label by position, and which pairs count as correct?

In this small example, the model is checked on five emails. Emails 1, 2, 4 and 5 have a prediction that matches the true label, so they count as correct. Email 3 is marked as changed because the true label is spam but the model predicted not spam, so it is an error. The diagram shows only one pair per email; the accuracy calculation depends on how many of these pairs match.

The Accuracy Formula

The formula is simply the definition written as a ratio. Count the predictions the model got right, divide by the total number of predictions it made, and the result is a value between 0 and 1. Accuracy is therefore a fraction, and it can also be expressed as a percentage by multiplying by 100.

Rule

Worked example

Computing accuracy for the five-email spam check

A spam filter labels five emails. Its predictions are spam, not spam, not spam, spam, not spam. The true labels are spam, not spam, spam, spam, not spam. What is the accuracy?

  1. 1
    Pair each prediction with its true label

    Position 1: predicted spam, true spam, match. Position 2: predicted not spam, true not spam, match. Position 3: predicted not spam, true spam, mismatch. Position 4: predicted spam, true spam, match. Position 5: predicted not spam, true not spam, match.

  2. 2
    Count the correct predictions

    Four pairs match, so the number of correct predictions is 4.

  3. 3
    Count the total predictions

    The model made five predictions, so the total is 5.

  4. 4
    Divide correct by total

    4 divided by 5 equals 0.8.

  5. 5
    Convert to a percentage if needed

    Multiplying 0.8 by 100 gives 80 percent.

Result

The accuracy is 0.8, or 80 percent. The same value appears in the earlier spam example with ten emails and eight correct labels, because 8 divided by 10 is also 0.8.

What a High Accuracy Score Hides

Accuracy is a single summary number. It tells you how often the model was right, but it does not reveal which predictions were wrong or what kinds of errors the model made. The calculation never asks which emails were misclassified. Eighty percent would be the same whether the two errors were harmless messages or dangerous ones. This is the key limitation: a good accuracy value answers how many predictions were correct, not whether the mistakes matter.

Consider a mailbox of ten emails, where nine are not spam and one is spam. A model that always predicts not spam is correct on all nine ordinary emails, so its accuracy is 9 out of 10, or 90 percent. Yet it never identifies the spam email. Compare that with a second model that makes two false alarms, flagging two ordinary emails as spam, but catches the one real spam email. Its accuracy is 8 out of 10, or 80 percent, which is lower, even though it does the job the filter was built for.

Correct predictions9 of 10 (90%)Correct predictions8 of 10 (80%)Spam emails caught0 of 1Spam emails caught1 of 1
Why can a model that always predicts the majority class reach high accuracy while learning nothing useful?

Mistakes When Reading Accuracy

  • Common mistake

    Treating a high accuracy value as proof that the model is good

    What it looks like

    A model that predicts not spam for every email scores 90 percent on a mailbox that is 90 percent ordinary mail.

    Why it fails

    Accuracy counts correct predictions but does not show which predictions were wrong. A model can score well while missing the cases that matter most.

    Do this instead

    Look at what kinds of errors the model makes, not only the single accuracy number.

  • Common mistake

    Dividing by the number of correct predictions instead of the total

    What it looks like

    Computing 8 divided by 8 instead of 8 divided by 10 when 8 of 10 predictions were correct.

    Why it fails

    The denominator must be the total number of predictions, so the result stays between 0 and 1.

    Do this instead

    Divide the count of correct predictions by the count of all predictions.

  • Common mistake

    Comparing a decimal with a percentage

    What it looks like

    Reading 0.8 in one report and 80 in another and deciding they describe different levels of accuracy.

    Why it fails

    A result of 0.8 and a result of 80 percent describe the same accuracy.

    Do this instead

    Convert both values to the same scale before comparing them.

  • Common mistake

    Assuming the errors were equally harmless without checking them

Practice Counting Accuracy

EASY

A classifier labels six images as cat or dog. Its predictions are cat, dog, dog, cat, cat, dog. The true labels are cat, dog, cat, cat, dog, dog. Pair the predictions with the true labels by position, count the correct predictions, and compute the accuracy as a decimal and as a percentage.

Hints
  • Compare position 1 with position 1, position 2 with position 2, and so on.
  • Mark each pair as a match or a mismatch before you count.
  • The denominator is the total number of predictions, which is 6.

Key Takeaways

  • Classification accuracy is the fraction of predictions that are correct: correct predictions divided by total predictions.
  • Accuracy is computed by pairing each prediction with its true label by position and counting the matches.
  • Accuracy is reported as a value between 0 and 1, or as a percentage by multiplying by 100, and both describe the same result.
  • Accuracy is a single summary number that does not show which predictions were wrong or what kinds of errors occurred, so a high value does not guarantee a useful model.

Related concepts

More concepts starting with E