What Is A Confusion Matrix?

Confusion Matrix

A confusion matrix provides a summary of all prediction results of a classification problem. Each result is shown with its corresponding number of correct and incorrect predictions (true positive, true negative, false positive, false negative), count values, and classification criteria. By providing a neat summary of all possible results, the confusion matrix lets you know the ways your classification model could get confused when making the predictions. It helps identify errors and the type of errors made by the model and thus helps improve the accuracy of the classification model.

The matrix is the first table you build after any classifier eval because raw accuracy misleads on imbalanced data. Counts in each cell translate directly into precision, recall, specificity, and false alarm rates. Slices and thresholds become visible once you stop collapsing everything into one number.

Key takeaways

  • A confusion matrix tabulates predicted vs actual classes for every example in an eval set.
  • Binary problems use a 2×2 layout (TP, TN, FP, FN); multi-class problems extend to k x k counts.
  • Derived metrics (precision, recall, F1) come from matrix cells, so the matrix is the source of truth.
  • Inspect matrices per slice (locale, product, time window) to find hidden failure modes.
  • Store matrices (or sufficient counts) with each model version for regression comparison.

Reading a binary confusion matrix

Layout for positive class “P” and negative class “N”:

Predicted P Predicted N
Actual P True positive (TP) False negative (FN)
Actual N False positive (FP) True negative (TN)
  • TP: model correctly flagged positives.
  • TN: model correctly cleared negatives.
  • FP: false alarm (predicted P, actual N).
  • FN: miss (predicted N, actual P).

From these counts:

  • Accuracy = (TP + TN) / total
  • Precision = TP / (TP + FP)
  • Recall (TPR) = TP / (TP + FN)
  • Specificity (TNR) = TN / (TN + FP)
  • FPR = FP / (FP + TN)

When negatives dominate, accuracy can stay high while recall on the rare class is zero. The matrix exposes that immediately.

Multi-class confusion matrices

With k classes, the matrix is k x k. Row i, column j holds the count of examples with true class i predicted as class j. Diagonal cells are correct; off-diagonals show which pairs confuse the model (cat vs dog, refund vs exchange).

Normalize by row to see recall per class (what fraction of true cats were predicted as each label). Normalize by column to see precision per predicted label. Heatmaps of off-diagonal mass guide data collection: if “billing” is often predicted as “shipping,” add labeled examples or features that separate them.

One-vs-rest binary matrices help when one class is rare or costly. Report a full k x k matrix plus focused 2 x 2 views for the business-critical class.

Thresholds and score-based classifiers

Many models output a score; you apply threshold t to assign class labels. One model checkpoint yields a family of confusion matrices as t sweeps from 0 to 1. Operating point selection is a policy choice:

  • High-stakes detection may require minimum recall even if FP count rises.
  • User-facing filters may cap FP rate to limit false blocks.

Plot precision-recall curves derived from sweeping t, but keep the matrix at the chosen t for stakeholder reports. Absolute FP and FN counts matter for staffing review queues.

Teams shipping image classification models with confidence document the matrix at the production threshold alongside calibration plots.

Using confusion matrices for debugging

Compare matrices across:

  • Model versions before promotion. Did FN drop enough to justify FP increase?
  • Data slices. Same global accuracy can hide broken segments.
  • Time windows. Weekly matrices detect gradual drift.
  • Label sources. Human labels vs heuristic labels vs LLM-as-judge labels.

Error analysis flows from the largest off-diagonal cells or the costliest cell (often FN in fraud, FP in moderation). Pull examples from those cells, cluster failure themes, then fix labels, features, or thresholds.

For LLM classification and agent routing, build matrices on eval sets with human adjudication. An LLM and agent evaluation platform helps keep eval splits, matrices, and traces linked so offline counts match what you see in production samples.

Confusion matrices in production monitoring

Fully labeled online traffic is rare. Options:

  • Delayed labels. Recompute matrices when outcomes arrive (chargeback, click, clinician review).
  • Human audit samples. Estimate FP/FN rates on reviewed subsets; confidence intervals widen.
  • Proxy labels. Imperfect but directional (user corrected the suggestion).

Even partial matrices beat accuracy-only dashboards. Track cell counts over time and alert when FP or FN rates move beyond control limits relative to a baseline week.

Version the label definition with the matrix. If “positive” meaning changes, historical matrices are not comparable without relabeling.

Limitations

Confusion matrices describe classification decisions, not ranking quality across all thresholds (use PR or ROC for that). They assume mutually exclusive classes; multi-label problems need per-label matrices. They summarize aggregate counts; they do not explain why errors happen without example-level follow-up.

Cost-sensitive problems may weight cells differently (FN cost 10x FP cost). Weighted metrics derive from the same table but reflect business asymmetry.

Store matrices in model registry metadata as part of AI model lifecycle management so rollback decisions compare like-for-like evals.

FAQ

What is a confusion matrix used for?

It summarizes how many predictions fall into each combination of predicted and actual class. You use it to compute metrics, compare models, choose thresholds, and prioritize error analysis.

How is a confusion matrix different from accuracy?

Accuracy is one number derived from the matrix. The matrix shows the error types accuracy hides, especially under class imbalance.

Can I use a confusion matrix for more than two classes?

Yes. Use a k x k table. Derived metrics extend via one-vs-rest or macro-averaging strategies; specify which you report.

How do LLM evals use confusion matrices?

When an LLM task maps to discrete labels (intent, toxicity, pass/fail rubric), tally human or judge labels against model outputs in a matrix. For generative tasks without discrete labels, use task-specific metrics instead.

Should I normalize the confusion matrix?

Use raw counts for business communication (how many errors). Use row or column normalization for diagnostic heatmaps showing per-class recall or precision patterns.

Don’t ship vibes.

Arize gives AI teams observability and evals to understand and improve agent performance.