# Precision, Recall and F1

> Precision, recall and F1 in plain words, with a card fraud example, why accuracy misleads and the trade-off, for your AI interview.

**In short:** Precision is the share of flagged cases that were right. Recall is the share of real cases the model caught. F1 is one score that combines the two, so a weak side can't hide behind a strong one.

AI and LLM interview question Q420, Classic ML and deep-learning basics. Beginner, very common (our estimate). From Venkat R. Murty's free Interview Kit: https://designervenkat.online/interview/ai/precision-recall-and-f1

## Say it in 30 seconds

Precision is how many of my flags were right. Recall is how many of the real cases I caught. F1 combines the two into one number, and it drops when either side is weak. On a made-up fraud model, precision was 66.7 percent, recall 80 percent and F1 72.7 percent. I never judge a rare-event model on accuracy alone.

## Say it in 2 minutes

Precision, recall and F1 are three scores for a yes or no model, such as a fraud alert or a spam filter. Precision asks: of the payments I flagged, how many really were fraud? Recall asks: of all the real frauds, how many did I catch? F1 gives one number that combines the two, so a model can't score well by being strong on only one side.

Here is a case from a mid-size bank's card team, with made-up numbers. The model checked 10,000 payments, and 100 were real fraud. It flagged 120. Eighty flags were right, and 40 were genuine customers who got blocked. So precision is 80 out of 120, about 67 percent. Recall is 80 out of 100, which is 80 percent. The model missed 20 frauds, and the F1 score is about 73 percent.

Accuracy looks excellent here, at 99.4 percent. But a model that never flags anything scores 99 percent too, and it catches no fraud at all. When one class is very rare, accuracy hides the misses, so I judge the model on recall and F1 for fraud.

The two scores pull against each other. Raise the threshold and the model flags less, so precision usually rises and recall falls. F1 is a harmonic mean, an average that drops towards the weaker number. I would set the threshold from what a missed fraud costs the bank against what a blocked customer costs, then report all three numbers.

## What are precision, recall and F1, in plain words?

Think about a triage nurse at a busy emergency desk. She flags patients who may need a doctor now. Some flags are right. Some are false alarms. And some patients who needed a doctor get past her. Precision and recall measure those two kinds of mistake.

Precision is about the flags: of everything flagged, how much was right? Recall is about the real cases: of everything that was real, how much did the model find? F1 is one number that combines them, so a model can't hide a weak side behind a strong one.

These scores are for yes or no models, called binary classifiers. The class you care about is called the positive class. For a fraud model, the positive class is fraud, and the genuine payments are the negatives.

- **Positive:** The class you are looking for, such as fraud.
- **True positive (TP):** A flag that was right. A real case the model caught.
- **False positive (FP):** A flag on a genuine case. A false alarm.
- **False negative (FN):** A real case the model missed.
- **F1:** One score that combines precision and recall, from 0 to 1.

## How do you calculate them, with real numbers?

Start from the confusion matrix, the table of right and wrong calls. Then divide. The numbers below are made up for a card-fraud model at a mid-size bank, checked on 10,000 payments with 100 real frauds.

*Diagram, From 120 flags to one F1 score: The 80 right flags sit in both ratios. The 40 wrong flags lower precision, and the 20 misses lower recall. F1 combines the two into one score.*
1. The 80 right flags are the top of precision.
2. The 40 wrong flags add to the bottom, so precision drops.
3. The same 80 are the top of recall.
4. The 20 misses add to the bottom, so recall drops.
5. Precision goes into the F1 score.
6. Recall goes into the F1 score too.

1. **Precision = TP / (TP + FP):** 80 / (80 + 40) = 66.7%. Of the 120 flags, 80 were right.
2. **Recall = TP / (TP + FN):** 80 / (80 + 20) = 80.0%. Of the 100 real frauds, 80 were caught.
3. **F1 = 2 × TP / (2 × TP + FP + FN):** 160 / 220 = 72.7%. This is the harmonic mean of 66.7% and 80%, an average that leans towards the smaller number.

Here is the same calculation in plain Python. It uses only the standard library, so you can run it anywhere.

```python
# A card-fraud model checked on 10,000 payments (made-up numbers).
# Positive = "this payment is fraud".
TP = 80    # fraud, flagged: the flag was right
FP = 40    # genuine, flagged: a customer is blocked
FN = 20    # fraud, not flagged: the fraud got through
TN = 9860  # genuine, not flagged

precision = TP / (TP + FP)
recall = TP / (TP + FN)
f1 = 2 * precision * recall / (precision + recall)
accuracy = (TP + TN) / (TP + TN + FP + FN)

print(f"precision {precision:.1%}")
print(f"recall    {recall:.1%}")
print(f"f1        {f1:.1%}")
print(f"accuracy  {accuracy:.1%}")

# A model that never flags anything: every fraud is missed.
print(f"never flags: accuracy {9900 / 10000:.1%}, recall {0:.1%}")
```

Output:

```text
precision 66.7%
recall    80.0%
f1        72.7%
accuracy  99.4%
never flags: accuracy 99.0%, recall 0.0%
```

## Why is accuracy not enough for fraud?

Accuracy counts every right call, flagged or not. Fraud is rare, so accuracy stays high even when the model misses most frauds. A model that never flags anything scores 99.0% accuracy on the same 10,000 payments, with recall of 0.0%. Our model scores 99.4% accuracy with recall of 80.0%. The accuracy gap is small. The recall gap matters most here.

*Diagram, Accuracy hides the misses: Both models score at least 99% on accuracy. Only recall shows which model finds the frauds, so judge a rare-event model on recall and F1.*
1. The baseline looks fine on accuracy but catches no fraud.
2. Our model's recall shows how many frauds it finds.

> **Tip:** Asked about accuracy on an imbalanced problem? Say it can look excellent while the model catches nothing, then name the metric that matches the cost of a miss.

## What is the trade-off between precision and recall?

Most models give each case a score, and a threshold turns that score into a yes or no. Raise the threshold and the model flags less: precision usually rises and recall falls. Lower it and recall rises, but more genuine payments get blocked. Google's crash course says it plainly.

> "As a result, precision and recall often show an inverse relationship, where improving one of them worsens the other." (Google Machine Learning Crash Course)

F1 is a harmonic mean, so it sits close to the weaker of the two. Google puts it this way: when precision and recall are far apart, F1 is similar to whichever is worse. A model with precision 0.9 and recall 0.1 gets an F1 of only 0.18, even though its precision looks strong.

Which side matters more is a business call. A missed fraud costs money, and a blocked customer costs a call and some goodwill. Google also notes that when both true positives and false positives are 0, precision comes out as NaN, which means not a number. scikit-learn returns 0 with a warning instead. So check the counts before you trust a score.

### How would you choose the metric for a sepsis alert?

A hospital wants an early warning for sepsis. A missed case is the costly error, so recall comes first. But every false alarm adds work for nurses.

So I would fix the recall target first, then check precision at that threshold, and track how many alerts each nurse gets per shift. Say it in one line: recall sets the safety floor, precision keeps the alerts trusted, and F1 is only a quick summary.

## What does the interviewer want to hear?

**A strong answer covers**

- [ ] Precision: of the flags, how many were right. Recall: of the real cases, how many were found.
- [ ] A worked example with real counts, read from a confusion matrix.
- [ ] Why accuracy misleads when one class is rare.
- [ ] F1 as a harmonic mean, so a weak side pulls it down.
- [ ] The threshold trade-off, and the business picks the balance.
- [ ] What a false positive and a false negative cost in the real world.

**Common traps**

- Trap: "Accuracy is the best metric for this." Better: On rare events accuracy can look great while the model finds no positives. Use recall and F1.
- Trap: "Precision and recall are the same thing." Better: They divide by different things. Precision divides by the flags, recall divides by the real cases.
- Trap: "F1 is just the average of the two." Better: It is a harmonic mean, which sinks towards the weaker side, so one low score is hard to hide.

## Can you spot the problem?

**Spot the problem:** A colleague shows this result for the fraud model. Which summary should make you ask for recall before it ships?

A. Precision 0.62, recall 0.91, F1 0.74 on the same test set.
B. Precision 0.62, recall 0.91, F1 0.74, threshold 0.5.
C. Accuracy: 99.6% on 10,000 payments, where only 0.4% are fraud.
D. Accuracy 99.6%, precision 0.62, recall 0.91 and F1 0.74.

**Answer:** C. Accuracy looks excellent only because almost every payment is genuine. The option that gives accuracy alone hides whether any fraud is caught. Ask for recall and F1 on the fraud class.

**Quick check:** A fraud team says the model catches 90% of real frauds. Which score is that?

A. Precision
B. Recall
C. F1
D. Accuracy

**Answer:** B. Recall is the share of real cases the model found. Precision counts the flags, F1 mixes the two, and accuracy counts every payment.

## Can you answer these from memory?

**Flash cards**

- **What is precision?** Of all the flags, the share that were really positive.
- **What is recall?** Of all the real positives, the share the model found.
- **What is F1?** One score: the harmonic mean of precision and recall.
- **Why is accuracy weak for fraud?** Almost every payment is genuine, so a model that flags nothing still scores high.
- **What does a higher threshold do?** Usually precision goes up and recall goes down.
- **What is the precision formula?** TP / (TP + FP): right flags over all flags.

## Likely follow-up questions

- Q421 ROC-AUC vs PR-AUC? (full answer coming)
- Q422 Train/validation/test splits? https://designervenkat.online/interview/ai/train-validation-test-splits
- Q424 What is data leakage? (full answer coming)
- Q425 How do you handle class imbalance? (full answer coming)
- Q430 What is a confusion matrix? (full answer coming)
- Q436 How do you tune hyperparameters? (full answer coming)

## Learn it in depth

- [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)
- [F1-Score: one number for two goals](https://designervenkat.online/learn/machine-learning/f1-score)
