Skip to content
mlmentorship

Confusion matrix and classification metrics

A confusion matrix counts predicted labels against actual labels. Read precision, recall and other fixed-threshold metrics from their correct populations.

Published · 15 min read ·Core ·Foundation
Reading depth

Summary

A confusion matrix counts predicted labels against actual labels. Rows and columns select different populations, so they answer different questions.

Suppose 1,000 held-out cases produce 98 alerts. Eight alerts are correct, and two positive cases receive no alert. How many alerts are useful? How many actual positives did the model find? Use mature labels and one fixed threshold for this example.

The binary confusion matrix

Predicted 0Predicted 1
Actual 0TN (True Neg)FP (False Pos, Type I)
Actual 1FN (False Neg, Type II)TP (True Pos)

Spatial intuition

Eight hits can mean high recall and low precision

Eight hits; two different questionsRecall divides eight by ten actual positives. Precision divides the same eight by forty predicted positives.Same cases. Same hits. Differentgroups.Actual +8 / 1080%Predicted +8 / 4020%8 green hits survive both selectionsChanging the denominator changes thequestion
Derivation

Precision: trust a positive prediction

Highlight the predicted-positive column.

ActualPred 0Pred 1
0900 TN90 FP
12 FN8 TP

precision = 8 / (8 + 90) ≈ 8.16%

Recall: find the actual positives

Highlight the actual-positive row.

ActualPred 0Pred 1
0900 TN90 FP
12 FN8 TP

recall = 8 / (8 + 2) = 80%

Read it this way: the same classifier has 80% recall and about 8.16% precision. The matrix shows many more false alarms than true alerts.
Check: How can the same eight true positives yield 80% recall but only about 8.16% precision?
Read it this way: Recall divides eight by ten actual positives. Precision divides the same eight by forty predicted positives.
Which extra cases enter precision but not recall?

Read the diagram’s positive prediction column: 90 false alerts plus eight true alerts. Precision is , or about 8.16%. Read the actual-positive row: two misses plus eight true alerts. Recall is .

The remaining 900 cases are true negatives. Check the total: . Accuracy is , although most alerts are false.

The denominator must count the population named in the question. Let . These metrics use the four counts at this threshold:

MetricFormulaMeaning
AccuracyOverall correctness
Precision (PPV)Of predicted positives, fraction correct
Recall (TPR, Sensitivity)Of actual positives, fraction caught
Specificity (TNR)Of actual negatives, fraction correctly rejected
F1Harmonic mean of precision and recall
FPRFalse alarm rate;
FNRMiss rate;
NPVNegative predictive value
Balanced accuracyAverage of recall on each class
Matthews Corr Coefsee belowSingle-number summary that handles imbalance

Matthews Correlation Coefficient (MCC):

When its denominator is nonzero, MCC ranges from to . Perfect agreement gives ; complete reversal gives . Zero means no observed binary correlation, not proof of a useful or random model. MCC uses all four marginal totals. It is not invariant to class prevalence.

For this table, . Specificity is . Balanced accuracy is . The table describes errors. Product costs determine whether those errors are acceptable.

Change the population

1. Find which actual class is missed

New task: compare recall across actual classes.

Picture and change: divide each row by its own total, rather than dividing a prediction column. Keep raw counts visible beside the normalized table.

Check: the actual-positive row becomes . The positive prediction column instead gives precision . They describe different populations and need not agree.

Use when: the question concerns coverage of an actual class.

2. Give each class equal weight

New task: prevent the majority class from dominating a recall summary.

Picture and change: calculate recall for each class, then average the two results. Do not pool their counts first.

Check: equal weighting gives balanced accuracy, about . Pooling correct predictions gives accuracy , because 990 of 1,000 cases are negative.

Use when: each class has equal decision importance, regardless of its frequency. Equal weighting still does not remove the need to inspect each class.

What this table cannot tell you

Two models can produce identical labels at threshold but order their scores differently. Their confusion matrices match, while ranking measures and log losses can differ. Use scores to evaluate those properties. One thresholded table cannot recover them.

No predicted positives gives precision denominator zero. No actual positives gives recall denominator zero. State the undefined-value convention and show the support. An assigned zero is a reporting policy, not an observed error rate.

Try a missing population

There are no positive predictions. Can precision prove that the model’s alerts are reliable?

Check your answer

No. There are no alerts to assess, so precision is undefined before applying a reporting convention. Report zero predicted support beside that convention.

Multi-class confusion matrix

For classes, use a table. Diagonal entries are correct predictions; other entries are confusions. For class , treat all other classes as negative and recover counts from row and column totals.

  • Macro: average the per-class metrics equally.
  • Micro: combine counts first, then calculate the metric.
  • Weighted: weight each per-class metric by its support.

In ordinary single-label multiclass classification, micro precision and recall both equal accuracy. Do not extend that identity to multilabel predictions.

Visualizing the confusion matrix

Label both axes explicitly. Use row normalization for recall and column normalization for precision. Keep counts available and use the same class order on both axes.

Threshold dependence

Increasing the positive-score threshold cannot increase TP or FP on the same examples. Precision can still move either way. Threshold sweeps need the scores, not only one matrix. Choose the operating point on validation data using the stated costs or constraints.

What to report

Report counts, per-class metrics, the operating threshold and the aggregation convention. Add score-based curves when they answer the decision question. State whether PR area means average precision or another integration rule. Scikit-learn provides confusion_matrix, classification_report, roc_auc_score and average_precision_score.

Common pitfalls

  • State the axis convention; libraries differ.
  • Use held-out data, not training performance, for generalization claims.
  • Do not select a threshold on the final test set.
  • Inspect class supports. One average can hide a failed class.