Concepts / Accuracy as a Classification Metric

Accuracy as a Classification Metric

Accuracy gives you one number summarizing how often a classifier is right.

  • Programming

Why One Accuracy Number Falls Short

Accuracy gives you one number summarizing how often a classifier is right. That number is useful, but it compresses everything the model did into a single value. Two classifiers can earn the same accuracy while making very different kinds of mistakes, and the single number cannot tell them apart. A confusion matrix opens that number up so you can see which kinds of mistakes a classifier makes.

We start from a realistic situation. A logistic regression model has already produced predictions on a held-out test set, and those predictions are stored in a variable named pred_logreg. The true labels for the same test samples are in y_test. Passing those two arrays to confusion_matrix turns them into a matrix of counts.

python

The output is a grid of four numbers. Before reading further, try to guess what the grid is organized around. Which two pieces of information is it counting against each other? Keep your guess in mind, because the next section answers it directly.

Reading the Grid's Orientation

The orientation of the matrix is the key to reading it. Each row is tied to the true class of a group of samples, and each column is tied to the class the model assigned to them. Every entry is a count of how many samples with a given true class received a given predicted class. The first row therefore describes everything that truly belonged to the not-nine group, split by what the model guessed. The second row describes everything that truly belonged to the nine group, split the same way.

Top-left cellrow: true not-nine, column:predicted not-nineTop-right cellrow: true not-nine, column:predicted nineBottom-left cellrow: true nine, column:predicted not-nineBottom-right cellrow: true nine, column:predicted nine
Which direction is the true class and which is the predicted class, and how do you find a given outcome's cell by its row and column position?

The diagram places the four cells in their matrix positions. The top row is the not-nine true class and the bottom row is the nine true class. The left column is the not-nine prediction and the right column is the nine prediction. Once you hold that grid in mind, reading the counts becomes a single question: which true class and which predicted class meet in a given cell?

This layout gives the matrix a useful property. The two cells on the main diagonal, running from top-left to bottom-right, are the cases where the true class and the predicted class agree. Those are the correct classifications. The two off-diagonal cells are the mistakes, and each one names both the class a sample really belonged to and the class the model wrongly chose for it.

Naming the Four Cells

Binary classification is easier to discuss once one class is designated as the positive class. In the nine-versus-rest task, declaring a nine the positive class lets the four cells receive standard names. The names describe whether the model was right or wrong, and which class it predicted.

True Positiveactual nine, predicted nineTrue Negativeactual not-nine, predictednot-nineFalse Positiveactual not-nine, predictednineFalse Negativeactual nine, predictednot-nine
How do the labels of the four cells follow from the row and column they sit in?

The names are built from two independent judgments. The first word says whether the prediction was correct, true or false. The second word says what the model output, positive or negative. A false negative is therefore a mistake in which the model said negative, and the mistake is that the sample was actually positive. Read this way, the two correct cells are the true positives and true negatives on the diagonal, and the two mistake cells are the false positives and false negatives off the diagonal.

Key point

The first word of a cell name tells you whether the model was right. The second word tells you what the model predicted. The true class is given by the row, and it is the row, not the name, that tells you what the sample actually was.

Finding Where the Model Diverges

Now consider the question the matrix is meant to answer: where does the model's output diverge from the true labels? Suppose you want to know how many nine-images the classifier missed entirely. You do not need to recount the test set. You need only the row for true nine samples and the column for predicted not-nine samples, which is the bottom-left cell. That cell is a false negative count, and it is the single place where a nine was reported as something else.

Worked example

Locating the divergent cell in an illustrative test set

An illustrative test set has 100 samples: 10 true nines and 90 true not-nines. The classifier's predictions are summarized by the matrix rows [85, 5] for true not-nines and [4, 6] for true nines. Which cell is the divergent cell for missed nines, and what does it count?

  1. 1
    Identify the row

    Missed nines all have a true class of nine, so we use the bottom row, the true nine row, which reads [4, 6].

  2. 2
    Identify the column

    A missed nine was predicted as not-nine, so we use the left column, the predicted not-nine column.

  3. 3
    Read the cell

    The bottom-left cell is 4. This is the false negative count.

  4. 4
    Interpret the count

    Four samples that truly were nines were classified as not-nine. These are the missed nines.

Result

The bottom-left cell, with value 4, counts the nine-images the classifier missed.

Top-left85Top-right5Bottom-left4 missed ninesBottom-right6
Which single off-diagonal cell counts the missed nines, and what does its value mean in plain terms?

The worked example shows a useful habit. Each off-diagonal cell answers one specific question. The top-right cell counts the not-nines the model called nines, and the bottom-left cell counts the nines the model called not-nines. Before trusting any summary, check which of these cells is largest, because the largest mistake often matters most for the task.

Computing Accuracy from the Matrix

The matrix gives you a direct route to accuracy. Accuracy is the number of correct predictions divided by the number of all samples. In terms of the four outcomes, the correct predictions are the true positives and true negatives, and the number of all samples is the sum of every cell in the matrix. The correct predictions sit on the main diagonal, so accuracy is the diagonal sum divided by the total of all entries.

Rule

Formula, step by step
  1. 1
    The numerator is TP + TN, the two diagonal cells where the prediction matched the true class.
  2. 2
    The denominator is the sum of all four cells, which counts every sample in the test set exactly once.
  3. 3
    The fraction is therefore the share of samples that landed on the diagonal.
Worked example

Computing accuracy for the illustrative matrix

Using the illustrative matrix with TN = 85, FP = 5, FN = 4, and TP = 6, what is the accuracy?

  1. 1
    Sum the diagonal

    TP + TN = 6 + 85 = 91 correct predictions.

  2. 2
    Sum all entries

    85 + 5 + 4 + 6 = 100 samples in total.

  3. 3
    Divide

    Accuracy = 91 / 100 = 0.91.

Result

The accuracy is 0.91, which is a single number that hides the fact that 4 of the 10 nines were missed.

Why Accuracy Alone Can Mislead

The matrix becomes most valuable when you compare models side by side. A useful comparison computes a confusion matrix for several fitted models using the same true labels, so each matrix can be read against a common reference. The source example compares two dummy baselines, a decision tree, and a logistic regression.

Reading across those matrices exposes behavior that a single score would not. The most frequent class model never produces a positive prediction, so its true positive count is zero and all 47 of its positive samples become false negatives. The other dummy model does predict positives, but it produces only 4 true positives against 42 false positives, so most of its positive predictions are wrong. The decision tree is much more sensible, with 23 true positives. Logistic regression is better than the tree on every count: more true positives and true negatives, and fewer false positives and false negatives.

  • Common mistake

    Reading rows as predicted classes and columns as true classes

    What it looks like

    Treating the bottom-left cell as predicted nine but actually not-nine.

    Why it fails

    Rows are tied to the true class and columns to the predicted class, so the labels run the other way.

    Do this instead

    Read the row first to find the true class, then the column to find the prediction.

  • Common mistake

    Assuming the diagonal always means the model is good

    Why it fails

    The diagonal counts correct predictions in total, but a model can have a large diagonal and still miss an entire class.

    Do this instead

    Check each off-diagonal cell separately to see which kind of mistake dominates.

  • Common mistake

    Confusing a false positive with a false negative because the first word of the name looks similar

  • Common mistake

    Reporting accuracy without the matrix when the classes are imbalanced

Checking Your Reading of a Matrix

EASY

Using the illustrative matrix with rows [85, 5] for true not-nines and [4, 6] for true nines, identify the cell that counts nines the model called not-nine, state its name, and compute the accuracy. Then explain why the accuracy alone does not reveal this error.

Hints
  • Find the true nine row first, then the predicted not-nine column.
  • The name starts with False or True depending on whether the model was right, and ends with Negative or Positive for what it predicted.
  • Add the diagonal values and divide by the sum of all four cells.

Key Takeaways

  • A confusion matrix is a two-by-two array where rows are the true classes and columns are the predicted classes, so each entry counts one specific kind of outcome.
  • The main diagonal holds the true positives and true negatives, which are correct classifications, while the off-diagonal cells hold the false positives and false negatives.
  • Each cell name combines two judgments: whether the prediction was correct (true or false) and what the model output (positive or negative).
  • Accuracy equals the diagonal sum divided by the sum of all entries, but two models with the same accuracy can make very different errors, which the individual cells reveal.

Related concepts

More concepts starting with A