Skip to content

What Is a Confusion Matrix? Definition and Examples

Quick answer

A confusion matrix explained: the four cells, how to read precision and recall from them, a worked ten-prediction example with code, and common mistakes.

What Is a Confusion Matrix? Definition and Examples

A confusion matrix is a table that compares a classification model's predictions with the true labels. For a yes/no problem it has four cells: true positives, false positives, true negatives and false negatives. It shows not only how often the model is right, but which kind of mistake it makes.

Key takeaways

  • A confusion matrix counts correct and wrong predictions for each class, in a grid.
  • The four binary cells are true positive, false positive, true negative and false negative.
  • Precision and recall are read from the cells and answer different questions.
  • Accuracy alone can mislead when one class is rare.

On this page:

MeasureFormulaQuestion it answers
Accuracy(TP + TN) / totalHow often is the model right overall?
PrecisionTP / (TP + FP)When it says positive, how often is it right?
Recall (sensitivity)TP / (TP + FN)Of all real positives, how many did it find?
Confusion matrixThe four counts themselvesWhere exactly does it go wrong?

What does a confusion matrix mean?

It means a count of predictions broken down by actual and predicted class. In the scikit-learn documentation (version 1.9.1), entry C(i,j) is the number of observations known to be in group i and predicted to be in group j. So rows are true labels and columns are predicted labels. The page warns that Wikipedia and other references may use a different convention for axes, so always check which way a table is drawn.

For binary classification, scikit-learn says C(0,0) is true negatives, C(1,0) false negatives, C(1,1) true positives and C(0,1) false positives. The scikit-learn model evaluation guide explains the wording: positive and negative refer to the classifier's prediction, and true and false refer to whether that prediction corresponds to the external judgment.

How does a confusion matrix work?

You compare each prediction with the truth and add one to the matching cell. Take ten made-up predictions, where 1 is the positive class. Actual: 1,1,1,1,0,0,0,0,0,0. Predicted: 1,1,0,0,1,0,0,0,0,0.

Predicted 0Predicted 1
Actual 0TN = 5FP = 1
Actual 1FN = 2TP = 2

Now the measures. According to scikit-learn, precision is the ability of the classifier not to label as positive a sample that is negative, and recall is its ability to find all the positive samples, with precision = tp / (tp + fp) and recall = tp / (tp + fn). Here precision = 2 / (2 + 1) = 0.67 and recall = 2 / (2 + 2) = 0.50. Accuracy, by the textbook formula, is (2 + 5) / 10 = 0.70.

from sklearn.metrics import confusion_matrix, precision_score, recall_score

y_true = [1,1,1,1,0,0,0,0,0,0]
y_pred = [1,1,0,0,1,0,0,0,0,0]
tn, fp, fn, tp = confusion_matrix(y_true, y_pred).ravel().tolist()
print(tn, fp, fn, tp)                     # 5 1 2 2
print(precision_score(y_true, y_pred))    # 0.666...
print(recall_score(y_true, y_pred))       # 0.5

Where is a confusion matrix used?

It is used to evaluate any classifier: fraud detection, spam filters, medical screening and customer churn. An illustrative Indian scenario with made-up numbers: of 1,000 transactions in a day, 30 are real frauds. A bank's model flags 100 transactions and catches 20 of the 30 frauds. That gives TP = 20, FP = 80, FN = 10 and TN = 890. Precision is 20 / 100 = 0.20, and recall is 20 / 30 = 0.67. A missed fraud is a false negative, and the bank must decide which error costs more.

What are the common mistakes and confusions?

  • Trusting accuracy on rare classes. In the example above, a model that says "not fraud" for every transaction gets 970 of 1,000 right, which is 97 percent accuracy, yet catches no fraud at all.
  • Reading the axes the wrong way round. scikit-learn puts true labels in rows; some references do the opposite.
  • Mixing up precision and recall. Precision looks at predicted positives; recall looks at actual positives.
  • Forgetting the positive class. Which label counts as positive changes every number.

Read about overfitting, feature engineering and linear regression, which predicts numbers rather than classes. For evaluation of language models, see LLM evaluation explained. For context, see machine learning vs deep learning and Python for data science.

Next step

Run the code, then change one prediction and predict how each cell moves before you re-run it. Next, read what a neural network is to see the kind of model these tables are often used to judge.

Frequently asked questions

What is a confusion matrix in simple words?

A confusion matrix is a table that counts how many predictions a classifier got right and wrong for each class. For two classes it shows true positives, false positives, true negatives and false negatives.

What do TP, FP, TN and FN stand for?

True positive, false positive, true negative and false negative. Positive or negative is the model's prediction, and true or false says whether that prediction matches the actual label.

What is the difference between precision and recall?

Precision is TP divided by TP plus FP: how often a positive prediction is right. Recall is TP divided by TP plus FN: how many of the real positives the model finds.

How do you read a confusion matrix in scikit-learn?

In scikit-learn, rows are true labels and columns are predicted labels. For binary problems, C(0,0) is true negatives, C(0,1) false positives, C(1,0) false negatives and C(1,1) true positives.

Why is accuracy not enough?

When one class is rare, a model can score high accuracy by always predicting the common class. For example, with 3 percent fraud, always predicting no fraud gives 97 percent accuracy and catches nothing.

Share LinkedIn X WhatsApp
×

Our Courses

Practice-Based Learning Tracks, Supercharged By A.I.