# What Is a Confusion Matrix?

> What a confusion matrix is, its four boxes, and why it shows the mistakes that a single accuracy score hides, explained simply for your AI interview.

**In short:** A confusion matrix is a table that compares a yes-or-no model's answers with what was really true, in four boxes: true positives, false positives, false negatives and true negatives. Scores like accuracy, recall and precision are all worked out from those four counts.

AI and LLM interview question Q430, Classic ML and deep-learning basics. Beginner, common (our estimate). From Venkat R. Murty's free Interview Kit: https://designervenkat.online/interview/ai/what-is-a-confusion-matrix

## Say it in 30 seconds

A confusion matrix is a table that counts every answer a yes-or-no model gives, against what was really true. It has four boxes: true positives, false positives, false negatives and true negatives. Accuracy hides the trouble. For a bank's fraud screen, the table shows how much real fraud gets missed and how many good customers get blocked. So I read the table first, then the scores.

## Say it in 2 minutes

Imagine a fraud analyst at a bank who checks last night's alarms. Some were real fraud and some were a customer buying something unusual. The analyst keeps a tally of right calls and wrong calls. A confusion matrix is that tally, written as a table.

Two boxes are right calls: true positives, where real fraud was blocked, and true negatives, where a genuine payment went through. Two boxes are mistakes. A false positive blocks a good customer. A false negative lets real fraud through, and that one costs money. Accuracy, recall and precision are all calculated from these four counts. With more than two answers, the table just gets more rows and columns, and the right calls stay on its diagonal.

My example has 10,000 card payments, and 120 of them are fraud, which is 1.2%. The screen catches 90 frauds, misses 30, and wrongly blocks 180 good payments. Accuracy looks strong at 97.9%, but recall is only 75% and precision is 33%. A screen that says fine to everything would score 98.8% accuracy and catch no fraud.

So in an interview I start with the four boxes, name the mistake that costs the most, and then pick the score that matches it. I also say that tools draw the table in different layouts, so I check the labels before I trust a number.

## What is a confusion matrix, in plain words?

A confusion matrix is a table for a model that answers yes or no, such as fraud or fine, or spam or not spam. In the tables here, each row is what really happened, and each column is what the model said. Every case lands in exactly one box, so the table shows where the model gets things mixed up. With more than two answers, the table grows to one row and one column per answer, and the right calls sit on its diagonal.

Think of a fraud analyst at a bank who checks last night's alarms. Some alarms were real fraud and some were a customer buying something unusual. The analyst keeps a tally of right and wrong calls. A card-fraud screen does the same job on every payment, and its tally is a confusion matrix.

- **Ground truth:** What really happened, as confirmed later by a record or a person.
- **Positive class:** The case you are looking for, such as fraud. It is called positive even though it is bad news.
- **Binary classifier:** A model that sorts each item into one of two groups.
- **Imbalanced data:** One group is rare, such as about 1.2% of card payments (roughly 1 in 83).

## What are the four boxes?

A two-answer model gives four possible results for each case, so the table has four boxes. Two boxes are right calls and two are mistakes. Read each name as two words. The first says whether the call was right (true) or wrong (false). The second says what the model said (positive or negative).

*Diagram, Four boxes for a fraud screen: Each of the 10,000 made-up card payments lands in one of four outcome boxes. The two green boxes are right calls, and the two orange outcome boxes (missed and wrongly blocked) are mistakes. The count sits in each box, so you can add them up.*
1. 120 of the payments were real fraud.
2. The other 9,880 were genuine customer payments.
3. The screen blocks 90 of the 120 real frauds.
4. The other 30 frauds get through, and money is lost.
5. It also blocks 180 good payments, and those customers get annoyed.
6. The other 9,700 good payments pass, as they should.

- **True positive (TP):** The model said fraud, and it was fraud. The fraud is stopped.
- **False positive (FP):** The model said fraud, but the payment was genuine. A good customer is blocked.
- **False negative (FN):** The model said fine, but it was fraud. The money leaves the account.
- **True negative (TN):** The model said fine, and it was genuine. The payment goes through.

## How do the four boxes become scores?

Take a bank's card-fraud screen that checks 10,000 card payments (made-up numbers). 120 of them are real fraud, and the other 9,880 are genuine. The four boxes give three common scores.

1. **Read the table:** 90 frauds caught, 30 missed, 180 good payments blocked, 9,700 let through.
2. **Accuracy: all right calls:** (90 + 9,700) / 10,000 = 97.9%. It looks excellent, and that is the trap.
3. **Recall: share of fraud caught:** 90 / 120 = 75%. One fraud in four gets through.
4. **Precision: share of blocks that were right:** 90 / (90 + 180) = 33%. Two out of three blocks hit a good customer.

Which score matters depends on the cost of each mistake. Accuracy hides both costs, because 9,700 easy passes make the total look good. On rare cases, a model can score high by always giving the common answer.

> "For heavily imbalanced datasets, where one class appears very rarely, say 1% of the time, a model that predicts negative 100% of the time would score 99% on accuracy, despite being useless." (Google Machine Learning Crash Course)

Here is the same table and the scores in plain Python, with made-up numbers. It uses only the standard library.

```python
# A bank's card-fraud screen checks 10,000 card payments (made-up numbers).
# Rows: what really happened. Columns: what the screen said.
TP, FN = 90, 30     # 120 real frauds: 90 caught, 30 missed
FP, TN = 180, 9700  # 9,880 genuine payments: 180 wrongly blocked, 9,700 let through

total = TP + FN + FP + TN
print(f"{'':<14}{'said fraud':>12}{'said fine':>12}")
print(f"{'really fraud':<14}{TP:>12}{FN:>12}")
print(f"{'really fine':<14}{FP:>12}{TN:>12}")
print(f"accuracy  {(TP + TN) / total:.1%}")
print(f"recall    {TP / (TP + FN):.1%}   (share of real fraud caught)")
print(f"precision {TP / (TP + FP):.1%}   (share of blocks that were fraud)")
print(f"always 'fine' gets {(TN + FP) / total:.1%} accuracy and catches 0% of fraud")
```

Output:

```text
                said fraud   said fine
really fraud            90          30
really fine            180        9700
accuracy  97.9%
recall    75.0%   (share of real fraud caught)
precision 33.3%   (share of blocks that were fraud)
always 'fine' gets 98.8% accuracy and catches 0% of fraud
```

## Which way round is the table?

Not every tool draws the table the same way. Google's course puts the real answer in the columns and the model's answer in the rows. scikit-learn, a Python machine learning library, does it the other way: real labels in the rows and predictions in the columns. The four counts are the same, only moved.

> "if you lay out the ground truth as columns and the model's prediction as rows, the following table, called a confusion matrix, is the result" (Google Machine Learning Crash Course)
>
> "Confusion matrix whose i-th row and j-th column entry indicates the number of samples with true label being i-th class and predicted label being j-th class." (scikit-learn)

By default scikit-learn sorts the labels, so in its binary layout the top left box holds true negatives and the bottom right box holds true positives. Our Python table lists fraud first, so there the top left box holds true positives.

> **Tip:** Check the labels before you trust a number. If you read the rows and columns the wrong way round, you divide by the wrong total and get a score that looks right but is not.

## What does the interviewer want to hear?

**A strong answer covers**

- [ ] A plain definition: a table of right and wrong calls, against what really happened.
- [ ] The four boxes, named in plain words.
- [ ] How accuracy, recall and precision come from the same four counts.
- [ ] Why accuracy misleads on rare cases, with a number.
- [ ] Layouts differ between tools, so read the labels first.
- [ ] The cost of each mistake decides which score to trust.

**Common traps**

- Trap: "A high accuracy means the model is good." Better: Check the rare class first. A model that always gives the common answer can look accurate.
- Trap: "The top left box is always the right answer." Better: It depends on the tool. Read the labels before you read the box.
- Trap: "A false positive and a false negative are the same mistake." Better: A missed fraud costs money, and a blocked customer loses trust. Which one costs more decides the score.

## Can you read the table?

**Find the costly miss:** Which box is the costly miss, where real fraud gets through?

A. Predicted fraud, really fraud: 90 (true positive)
B. Predicted fine, really fraud: 30 (false negative)
C. Predicted fraud, really fine: 180 (false positive)
D. Predicted fine, really fine: 9,700 (true negative)

**Answer:** B. A false negative is real fraud that the model called fine. That is the box where money leaves the bank, so recall is the score to watch.

**Quick check:** A fraud screen scores 97.9% accuracy on data where only 1.2% of payments are fraud. What should you check next?

A. Accuracy again, on a bigger sample of payments
B. Recall on fraud, worked out from the four boxes
C. How many rows the table has
D. How fast the screen runs

**Answer:** B. Accuracy looks high because genuine payments dominate. Recall shows how much real fraud is caught, and it comes straight from the table.

## Can you answer these from memory?

**Flash cards**

- **What is a confusion matrix?** A table of right and wrong calls, against what really happened.
- **What is a false negative?** A real positive that the model called negative. In fraud, a missed fraud.
- **What is recall?** Share of real positives caught: TP / (TP + FN).
- **What is precision?** Share of positive calls that were right: TP / (TP + FP).
- **Why can accuracy mislead?** On rare cases, always giving the common answer scores high.
- **Which way are the rows?** It depends on the tool. Check the labels before reading any number.

## Likely follow-up questions

- Q420 Precision, recall and F1? https://designervenkat.online/interview/ai/precision-recall-and-f1
- Q421 ROC-AUC vs PR-AUC? https://designervenkat.online/interview/ai/roc-auc-vs-pr-auc
- Q425 How do you handle class imbalance? https://designervenkat.online/interview/ai/how-do-you-handle-class-imbalance
- Q413 Overfitting vs underfitting? https://designervenkat.online/interview/ai/overfitting-vs-underfitting
- Q253 Exact match, accuracy and F1 for LLM tasks? https://designervenkat.online/interview/ai/exact-match-accuracy-and-f1-for-llm-tasks
- Q278 How do you evaluate an LLM classifier (confusion matrix)? https://designervenkat.online/interview/ai/how-do-you-evaluate-an-llm-classifier-confusion-matrix

## Learn it in depth

- [Confusion matrix: the table behind scores](https://designervenkat.online/learn/machine-learning/confusion-matrix)
- [Precision: is a yes really a yes?](https://designervenkat.online/learn/machine-learning/precision)
- [Recall: how many real yeses you catch](https://designervenkat.online/learn/machine-learning/recall)
