Confusion Matrix: Every Metric Worked Out from One Example
A confusion matrix is a table that counts a classifier's predictions against the true labels: true positives (TP), false positives (FP), false negatives (FN) and true negatives (TN). Almost every classification metric in an exam question is a ratio of these four cells, so if you can fill the matrix correctly, the numerical is mechanical. AI, Machine Learning & Data Analytics carries 10% of the NSCT according to HEC's Areas of Competencies, and its 2026 accuracy was 50.8%, per the HEC NSCT report and APP.
The definitions of precision, recall, F1, ROC-AUC and cross-validation are on the Model Evaluation & Validation page. This page goes further: matrix layouts, the full metric set, thresholds, prevalence, multi-class averaging, and working backwards from metrics to counts. Every number was computed in Python.
The method, step by step
- Identify the positive class. It is the class you care about catching (spam, fraud, disease), not necessarily the bigger one.
- Check the axes. Are rows actual and columns predicted, or the reverse? scikit-learn uses rows = actual, columns = predicted, in sorted label order, so a 0/1 problem gives [[TN, FP], [FN, TP]].
- Label the four cells before you compute anything.
- Write the formula, then substitute. Each metric divides by a row total, a column total or the grand total.
- Sanity-check: TP + FN = actual positives, TP + FP = predicted positives, and all four cells sum to n.
The running example
A spam filter is tested on 1,000 emails, 100 of which are spam (positive).

| Predicted spam | Predicted not spam | Total | |
|---|---|---|---|
| Actual spam | TP = 85 | FN = 15 | 100 |
| Actual not spam | FP = 30 | TN = 870 | 900 |
| Total | 115 | 885 | 1,000 |
Every metric from one matrix
| Metric | Formula | Value |
|---|---|---|
| Accuracy | (TP + TN) / n | 955 / 1,000 = 95.5% |
| Error rate | (FP + FN) / n | 4.5% |
| Precision (PPV) | TP / (TP + FP) | 85 / 115 = 73.91% |
| Recall (sensitivity, TPR) | TP / (TP + FN) | 85 / 100 = 85% |
| Specificity (TNR) | TN / (TN + FP) | 870 / 900 = 96.67% |
| False positive rate | FP / (FP + TN) | 3.33% |
| False negative rate | FN / (FN + TP) | 15% |
| Negative predictive value | TN / (TN + FN) | 870 / 885 = 98.31% |
| F1 | 2TP / (2TP + FP + FN) | 170 / 215 = 79.07% |
| Balanced accuracy | (recall + specificity) / 2 | 90.83% |
| MCC | (TP·TN − FP·FN) / √(product of the four margins) | 0.768 |
Reading the table: recall and FNR come from the actual-positive row. Specificity and FPR come from the actual-negative row. Precision comes from the predicted-positive column and NPV from the predicted-negative column. Recall + FNR = 1 and specificity + FPR = 1.
F-beta weights recall β times as much as precision: Fβ = (1 + β²)PR / (β²P + R). Here F2 = 82.52% (recall-leaning) and F0.5 = 75.89% (precision-leaning). F1 sits between them at 79.07%.
The Matthews correlation coefficient (MCC) runs from −1 to +1 and uses all four cells, so it stays honest on imbalanced data where accuracy does not.
Building the matrix from predictions
Ten cases with true labels and model scores; predict positive if score ≥ threshold.
| Score | 0.95 | 0.88 | 0.80 | 0.72 | 0.65 | 0.58 | 0.45 | 0.38 | 0.30 | 0.12 |
|---|---|---|---|---|---|---|---|---|---|---|
| True label | 1 | 1 | 0 | 1 | 0 | 1 | 0 | 0 | 1 | 0 |
Threshold 0.5: the top six scores are predicted positive. Four of them are actually 1 (TP = 4) and two are 0 (FP = 2). Of the bottom four, one is a 1 (FN = 1) and three are 0 (TN = 3).
Threshold 0.75: only the top three are positive, so TP = 2, FP = 1, FN = 3 and TN = 4.
| Threshold | TP | FP | FN | TN | Precision | Recall | F1 |
|---|---|---|---|---|---|---|---|
| 0.50 | 4 | 2 | 1 | 3 | 66.67% | 80% | 72.73% |
| 0.75 | 2 | 1 | 3 | 4 | 66.67% | 40% | 50% |
Raising the threshold always lowers (or keeps) recall, because fewer cases are called positive. Precision usually rises but does not have to. Here it stayed at 66.67%. That is why exam options saying "precision must increase" are wrong.
Prevalence changes precision, not recall
Suppose a test has 90% sensitivity and 95% specificity, used on 10,000 people.
| Prevalence | TP | FP | Precision |
|---|---|---|---|
| 1% | 90 | 495 | 15.38% |
| 10% | 900 | 450 | 66.67% |
| 50% | 4,500 | 250 | 94.74% |
Sensitivity and specificity are fixed properties of the test. Precision depends on how common the condition is. For rare conditions, most positives are false alarms even with a good test.
Multi-class confusion matrices
A ticket classifier sorts 150 support tickets (rows actual, columns predicted):
| Bug | Feature | Question | |
|---|---|---|---|
| Bug | 50 | 6 | 4 |
| Feature | 5 | 30 | 5 |
| Question | 2 | 8 | 40 |
For each class, treat it as "positive" and every other class as "negative":
| Class | Precision (column) | Recall (row) | F1 |
|---|---|---|---|
| Bug | 50 / 57 = 87.72% | 50 / 60 = 83.33% | 85.47% |
| Feature | 30 / 44 = 68.18% | 30 / 40 = 75% | 71.43% |
| Question | 40 / 49 = 81.63% | 40 / 50 = 80% | 80.81% |
- Macro average (simple mean over classes): precision 79.18%, recall 79.44%, F1 79.24%.
- Micro average pools all cells. For single-label problems it equals accuracy: 120 / 150 = 80%.
- Weighted F1 weights each class by its support (row total): 80.17%.
Macro averaging treats a rare class as equal to a common one, so use it when small classes matter.
Working backwards from metrics
Example. Recall is 0.75, precision is 0.6, and there are 80 actual positives. Find FP.
TP = 0.75 × 80 = 60, so FN = 20. Precision 0.6 means TP + FP = 60 / 0.6 = 100, so FP = 40.
Speed tricks
- F1 shortcut: 2TP / (2TP + FP + FN) avoids computing precision and recall first.
- The harmonic mean sits close to the smaller value. If precision is 0.9 and recall is 0.3, F1 must be near 0.45, not 0.6.
- Imbalanced data: if positives are rare, accuracy is almost the same as specificity. Ignore it and look at precision, recall or MCC.
- Swapping the positive class swaps precision with NPV and recall with specificity.
- Rows add to actual counts. If the stem gives the positive count, you already know TP + FN.
Common mistakes
- Reading a matrix with predictions in rows as if rows were actual classes. This turns precision into recall.
- Averaging precision and recall arithmetically instead of harmonically.
- Assuming that raising the threshold must raise precision.
- Reporting high accuracy on imbalanced data as evidence of a good model.
- Mixing up macro and micro averages in multi-class questions.
- Forgetting that FPR = 1 − specificity, not 1 − precision.
Practice set
Use TP = 30, FP = 10, FN = 20, TN = 140 for questions 1 to 4.
- Precision and recall?
- F1?
- Specificity?
- Accuracy?
- Precision 0.6 and recall 0.9. What is F1?
- A test has 80% sensitivity and 90% specificity, and 5% of 1,000 people have the condition. What is the precision?
- Recall is 0.8 and there are 50 actual positives. How many false negatives are there?
- Recall is 0.7 and specificity is 0.9. What is the balanced accuracy?
- A 2 × 2 matrix (rows actual) is [[18, 2], [4, 26]]. What is the accuracy?
Show answers
- 75% and 60%. 30 / 40 and 30 / 50.
- 66.67%. 60 / (60 + 10 + 20).
- 93.33%. 140 / 150.
- 85%. 170 / 200.
- 0.72. 2 × 0.54 / 1.5.
- 29.63%. TP = 40, FP = 95, so 40 / 135.
- 10. TP = 40.
- 0.8. (0.7 + 0.9) / 2.
- 88%. 44 / 50.
Practise
The quiz below has 20 confusion matrix MCQs, filterable by tier and difficulty. Every metric was computed in Python, and the code question was run.
Practise: 20 questions
FAQs
Are rows actual or predicted in a confusion matrix?
There is no universal rule. scikit-learn uses rows for actual and columns for predicted, and many textbooks do too, but some books and tools transpose it. Always read the axis labels. In an exam, the stem or the table headings should tell you.
When should I use F1 instead of accuracy?
Use F1 when the positive class is rare or when false positives and false negatives both matter. With 2% positives, a model that predicts "negative" every time scores 98% accuracy and an F1 of 0. F1 ignores TN, which is exactly the cell that inflates accuracy.
What is the difference between sensitivity and specificity?
Sensitivity (recall) is the share of actual positives caught, TP / (TP + FN). Specificity is the share of actual negatives correctly cleared, TN / (TN + FP). Medical screening usually prioritises sensitivity; confirmatory tests prioritise specificity.
Is MCC better than F1?
MCC uses all four cells and is symmetric, so it does not change if you swap the positive class. F1 ignores TN. For balanced comparisons on imbalanced data, many practitioners prefer MCC, but F1 is still the more common exam metric.
Next step
Revise the definitions in Model Evaluation & Validation, then study overfitting vs underfitting. Return to the AI & ML MCQ hub, or time yourself in the NSCT mock test.