Confusion Matrix: Every Metric Worked Out from One Example

By the NSCT Exam Editorial Team · Updated 2026-11-24 · Facts checked against official sources on 2026-11-24

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

  1. Identify the positive class. It is the class you care about catching (spam, fraud, disease), not necessarily the bigger one.
  2. 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]].
  3. Label the four cells before you compute anything.
  4. Write the formula, then substitute. Each metric divides by a row total, a column total or the grand total.
  5. 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).

Confusion matrix for a spam filter: TP 85, FN 15, FP 30, TN 870, with precision 73.91%, recall 85%, accuracy 95.5% and F1 79.07%.
The page's spam-filter example (1,000 emails) with the key metrics computed from the four cells.
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 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

Common mistakes

Practice set

Use TP = 30, FP = 10, FN = 20, TN = 140 for questions 1 to 4.

  1. Precision and recall?
  2. F1?
  3. Specificity?
  4. Accuracy?
  5. Precision 0.6 and recall 0.9. What is F1?
  6. A test has 80% sensitivity and 90% specificity, and 5% of 1,000 people have the condition. What is the precision?
  7. Recall is 0.8 and there are 50 actual positives. How many false negatives are there?
  8. Recall is 0.7 and specificity is 0.9. What is the balanced accuracy?
  9. A 2 × 2 matrix (rows actual) is [[18, 2], [4, 26]]. What is the accuracy?
Show answers
  1. 75% and 60%. 30 / 40 and 30 / 50.
  2. 66.67%. 60 / (60 + 10 + 20).
  3. 93.33%. 140 / 150.
  4. 85%. 170 / 200.
  5. 0.72. 2 × 0.54 / 1.5.
  6. 29.63%. TP = 40, FP = 95, so 40 / 135.
  7. 10. TP = 40.
  8. 0.8. (0.7 + 0.9) / 2.
  9. 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

Interactive quiz loading. The first 10 questions are shown below.

  1. A screening test flags a healthy person as having the disease. Which confusion-matrix cell does this case fall in?

    1. False negative (Type II error)
    2. True positive
    3. False positive (Type I error)
    4. True negative
    Show answer

    Answer: C. False positive (Type I error)

    The model said 'positive' (disease) but the truth is negative (healthy), so it is a false positive. In hypothesis-testing terms that is a Type I error. A false negative would be a sick person cleared as healthy.

  2. Which two cells does recall use?

    1. TP and FP
    2. TP and TN
    3. TP and FN
    4. FP and TN
    Show answer

    Answer: C. TP and FN

    Recall = TP / (TP + FN): of all the actual positives, how many did the model catch? Precision uses TP and FP, and specificity uses TN and FP.

  3. For binary labels 0 and 1, scikit-learn's confusion_matrix(y_true, y_pred) returns a 2 × 2 array. Where is the true-positive count?

    1. Top-left, at [0][0]
    2. Top-right, at [0][1]
    3. Bottom-right, at [1][1]
    4. Bottom-left, at [1][0]
    Show answer

    Answer: C. Bottom-right, at [1][1]

    scikit-learn puts actual classes in rows and predicted classes in columns, in sorted label order, so the layout is [[TN, FP], [FN, TP]]. Label 1 is second, so TP sits at [1][1]. Textbooks often draw the positive class first, which is why you must check the axes.

  4. The number of true negatives doubles while TP, FP and FN stay the same. Which metric is unchanged?

    1. Accuracy
    2. Recall
    3. Specificity
    4. Negative predictive value
    Show answer

    Answer: B. Recall

    Recall = TP / (TP + FN) does not use TN, so it cannot change. Accuracy, specificity and NPV all include TN. Precision and F1 would also be unchanged, which is why they suit imbalanced data where TN is huge.

  5. In a single-label multi-class problem, micro-averaged precision always equals which quantity?

    1. Macro-averaged precision
    2. Overall accuracy
    3. The precision of the largest class
    4. Micro-averaged specificity
    Show answer

    Answer: B. Overall accuracy

    Micro-averaging pools every class's TP and FP. Each wrong prediction is exactly one FP (for the predicted class), so total TP + FP = all samples and micro precision = correct / total = accuracy. Micro recall and micro F1 are equal to it too.

  6. Specificity is also known as:

    1. True positive rate
    2. True negative rate
    3. Positive predictive value
    4. False positive rate
    Show answer

    Answer: B. True negative rate

    Specificity = TN / (TN + FP), the share of actual negatives correctly identified: the true negative rate. The true positive rate is recall, the positive predictive value is precision, and FPR = 1 − specificity.

  7. A fraud model is tested on 2,000 transactions: TP = 36, FP = 64, FN = 12, TN = 1,888. What is the precision?

    1. 36%
    2. 75%
    3. 1.8%
    4. 96.7%
    Show answer

    Answer: A. 36%

    Precision = TP / (TP + FP) = 36 / (36 + 64) = 36 / 100 = 36%. Only about a third of fraud alerts are real fraud. 75% is the recall.

  8. A fraud model is tested on 2,000 transactions: TP = 36, FP = 64, FN = 12, TN = 1,888. What is the recall?

    1. 36%
    2. 25%
    3. 97.4%
    4. 75%
    Show answer

    Answer: D. 75%

    Recall = TP / (TP + FN) = 36 / (36 + 12) = 36 / 48 = 75%. The model catches three-quarters of the actual fraud. 25% is the false negative rate.

  9. A fraud model is tested on 2,000 transactions: TP = 36, FP = 64, FN = 12, TN = 1,888. What is the F1 score, to two decimal places?

    1. 48.65%
    2. 55.50%
    3. 51.43%
    4. 96.20%
    Show answer

    Answer: A. 48.65%

    F1 = 2TP / (2TP + FP + FN) = 72 / (72 + 64 + 12) = 72 / 148 = 48.65%. This equals the harmonic mean of precision (0.36) and recall (0.75). 55.50% is their arithmetic mean, which overstates the weaker metric.

  10. A fraud model is tested on 2,000 transactions: TP = 36, FP = 64, FN = 12, TN = 1,888. What is the accuracy?

    1. 94.4%
    2. 97.6%
    3. 96.2%
    4. 95.0%
    Show answer

    Answer: C. 96.2%

    Accuracy = (TP + TN) / total = (36 + 1,888) / 2,000 = 1,924 / 2,000 = 96.2%. It looks excellent only because legitimate transactions dominate; precision is just 36%.

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.