Skip to main content
Data Science Practice Tests

A Confusion Matrix Shows Where a Model Gets It Wrong

Back to category

A confusion matrix is a simple table that shows how a classification model got its answers right and wrong. It compares the true labels with the predicted labels, so the pattern of errors becomes visible instead of hidden inside one score.

What the table is doing

In classification, a model makes a guess for each case. The confusion matrix puts those guesses into a grid. Each cell counts one kind of outcome.

For binary classification, there are four basic outcomes. A true positive is a correct positive guess. A true negative is a correct negative guess. A false positive is a wrong positive guess. A false negative is a wrong negative guess.

That sounds dry, but it is the heart of the idea. A single accuracy number can hide useful detail. A confusion matrix shows where the model is failing.

Why it matters

A model can score well and still miss the point. If one class is common, accuracy can look fine even when the model keeps missing the rare class. The confusion matrix makes that visible.

It also shows the type of mistake. Some errors are worse than others in real work. Missing a fraud case is different from flagging a normal payment by mistake. Missing a sick patient is different from raising a false alarm. The matrix does not judge which error matters more, but it shows the split clearly.

For a binary classifier, the grid is usually read like this:

  • Rows show the true class.
  • Columns show the predicted class.
  • The diagonal cells are correct predictions.
  • The off-diagonal cells are mistakes.

In scikit-learn, this convention is used by the confusion_matrix function from sklearn.metrics. The result is an array where each row and column index matches a class label, and the entries count how often the model placed samples in each true-predicted pair.[1]

A small example

Imagine eight items. The true labels are:

[0, 1, 0, 1, 1, 0, 0, 1]

The model predicts:

[0, 1, 0, 0, 1, 1, 0, 1]

Now count the outcomes.

  • True class 0, predicted 0 happens 3 times.
  • True class 0, predicted 1 happens 1 time.
  • True class 1, predicted 0 happens 1 time.
  • True class 1, predicted 1 happens 3 times.

That gives this matrix:

[[3, 1],
 [1, 3]]

This table tells a fuller story than accuracy alone. The model got six out of eight correct. But it still made one false positive and one false negative. That matters when the cost of each error is different.

How Python creates it

In Python, the common way is to use confusion_matrix from sklearn.metrics. The function takes two lists or arrays. One holds the true labels. The other holds the predicted labels.[1]

Example:

from sklearn.metrics import confusion_matrix

y_true = [0, 1, 0, 1, 1, 0, 0, 1]
y_pred = [0, 1, 0, 0, 1, 1, 0, 1]

cm = confusion_matrix(y_true, y_pred)
print(cm)

The output is the matrix of counts. For the sample above, the counts fall into the four cells that match the true and predicted classes.[1]

That is enough for the basic case. If the classes are not binary, the same idea still works. The table just gets larger. Each row still represents the actual class, and each column still represents the predicted class.[1]

What the matrix does not tell you

A confusion matrix is useful, but it has limits. It is a count table, not a full diagnosis. It does not explain why the model made a mistake. It does not tell you whether the data was noisy, biased, sparse, or poorly labeled.

It also does not replace other metrics. Precision, recall, and F1 score are derived from the same counts and can be easier to compare across models. The matrix is the raw shape of the error. The metrics are the summary of that shape.

That is why the matrix is often the first stop, not the last one. It shows the structure of the problem. Then the other metrics turn that structure into numbers that are easier to compare.

The practical takeaway

A confusion matrix is what turns model results into a visible pattern of correct and incorrect predictions. It is plain, but it is honest. It shows whether the model is missing positives, overcalling them, or balancing the two badly.

After this lesson, the reader can look at a classification model and read its error pattern from the table. They can also create one in Python and understand what each cell means instead of treating the output as a block of numbers.

That is the kind of small technical idea I trust. It explains the tradeoff, shows the mistake, and gives a real next step. That is also the promise behind The Dravelo Field Notes, with one practical technical idea, one learning decision, and one useful network resource each edition.