# Data Interview Must-Know: Precision vs Recall vs F1 (Stop Mixing Them Up)

<p align="center">
  <img src="https://bugfree-s3.s3.amazonaws.com/mermaid_diagrams/image_1776100811056.png" alt="Precision vs Recall vs F1" style="max-width:700px; width:100%; height:auto;"/>
</p>

## Precision vs Recall vs F1 — interview-ready explanations

When you're asked about evaluation metrics in interviews, give clear definitions, show the math, and explain when to prefer each metric. Below is a compact, practical guide you can recite or use as notes.

### Quick confusion-matrix recap
- TP (True Positive): model predicts positive and it is positive
- FP (False Positive): model predicts positive but it's negative
- TN (True Negative): model predicts negative and it's negative
- FN (False Negative): model predicts negative but it's positive

Use these to compute precision and recall.

### Formulas and plain language
- Precision = TP / (TP + FP)
  - Of the examples predicted positive, how many are actually positive?
  - Use when false positives are costly (e.g., marking a legitimate email as spam, charging a user for fraud when they’re innocent).

- Recall = TP / (TP + FN)
  - Of the actual positive examples, how many did you catch?
  - Use when false negatives are costly (e.g., missing disease cases, failing to detect fraudulent transactions).

- F1 score = 2 * (Precision * Recall) / (Precision + Recall)
  - Harmonic mean of precision and recall. It rewards models that balance the two and penalizes extreme imbalance (very high precision and very low recall or vice versa).
  - Common when classes are imbalanced and you want a single-number summary.

### Intuition: precision vs recall trade-off
- Increasing the decision threshold often raises precision but lowers recall (you're stricter about predicting positives).
- Lowering the threshold raises recall but may reduce precision (you predict positives more liberally).
- Choose the operating point based on business costs: which error hurts more — FP or FN?

### When to prefer each metric (practical examples)
- Precision-first scenarios
  - Email spam filter: avoid marking real email as spam (FP costly).
  - Fraud investigations: minimize false accusations.

- Recall-first scenarios
  - Medical screening: you want to find as many true cases as possible (FN costly).
  - Disease outbreak detection: early detection is critical.

- F1 / balanced metric scenarios
  - Rare-event detection like fraud or defect detection where both catching positives and avoiding false alarms matter.
  - When you need a single metric for model selection and classes are imbalanced.

### Interview tips
- State the formulas and the confusion-matrix definitions first — interviewers expect that.
- Give a real-world example where you'd prioritize precision and another where you'd prioritize recall.
- Mention threshold tuning (precision–recall trade-off) and that F1 is the harmonic mean, not an arithmetic mean — explain why that matters briefly (it punishes extreme imbalance).
- If relevant, note other metrics: precision-recall curve, average precision, ROC-AUC (different focus), and macro/micro averaging for multiclass problems.

### Cheat sheet (one-liners to memorize)
- Precision: correctness among predicted positives.
- Recall: completeness among actual positives.
- F1: balance between precision and recall (harmonic mean).

Use these concise lines and one or two examples in interviews — you’ll sound precise, practical, and ready to apply metrics to real problems.
