Confusion Matrix Calculator

Confusion Matrix Calculator

Paste a confusion matrix with any number of classes to get precision, recall and F1 for each class, their macro and weighted averages, overall accuracy and Cohen's kappa.

Rows = actual class · columns = predicted class

One row per actual class. An optional first line of names labels the classes.

Paste a square matrix of counts

What Is a Confusion Matrix?

A confusion matrix is a table that counts how often each actual class was predicted as each possible class. Correct predictions sit on the diagonal; everything off the diagonal is a mistake, and its position shows exactly which classes get confused.

For each class you can read off precision (of the items predicted as this class, how many were right?) and recall (of the items truly in this class, how many were found?). F1 combines them.

Summarising many classes takes care. A macro average treats every class equally, a weighted average reflects class sizes, and Cohen's kappa discounts the agreement you'd get by chance.

Confusion Matrix Formulas

As given in Sokolova & Lapalme (2009) and Cohen (1960). See Sources.

1. Per class i

precision = mᵢᵢ ÷ column total · recall = mᵢᵢ ÷ row total · F1 = 2PR ÷ (P + R)

2. Averages

macro = mean over classes · weighted = mean weighted by support · micro = accuracy

3. Cohen's kappa

κ = (pₒ − pₑ) ÷ (1 − pₑ), pₑ = Σ (row total × column total) ÷ N²

pₒ is the observed accuracy; pₑ is the accuracy expected if predictions were random with the same class frequencies.

Worked Example: A Cat, Dog and Rabbit Classifier

Given:
An image model is tested on 120 photos: 50 cats, 50 dogs, 20 rabbits.
Step 1:
The matrix (rows actual, columns predicted):
Cat 45 4 1 Dog 6 38 6 Rabbit 2 3 15
Step 2:
Rabbit scores:
precision 15 ÷ 22 = 68.2% · recall 15 ÷ 20 = 75% · F1 71.4%
Step 3:
Summaries:
accuracy 81.7% · macro F1 79.6% · weighted F1 81.6% · κ = 0.709
Result:
Macro F1 79.6%, dragged down by rabbits

The matrix shows where the trouble is: 6 dogs were called rabbits. The weighted average hides this because rabbits are a small class; the macro average exposes it.

Macro, Micro and Weighted Averages Compared

The pet classifier above, three ways.

Averaging methods for the example classifier
AveragePrecisionRecallF1Use when
Macro79.2%80.3%79.6%Every class matters equally
Weighted81.9%81.7%81.6%Bigger classes matter more
Micro81.7%81.7%81.7%Every item matters equally

Common Confusion Matrix Mistakes

  • Transposing the matrix. Some tools put actual classes in columns. Swapping rows and columns swaps precision and recall.
  • Reporting only accuracy. With uneven classes, a model can ignore a small class entirely and still score well.
  • Not saying which average. Macro and weighted F1 can differ by several points; always name the one you report.
  • Ignoring the off-diagonal pattern. Which classes are confused with which is often more useful than any single score.

Confusion Matrix FAQs

Which way round should the matrix go?

Here, rows are actual classes and columns are predicted ones, the convention used by scikit-learn. Check your source before pasting.

What is support?

The number of items actually in each class: the row total.

Why does micro-average equal accuracy?

In single-label classification, every mistake is one false positive for one class and one false negative for another, so the totals match.

What is a good kappa?

A common rough guide: above 0.8 is excellent, 0.6–0.8 substantial, 0.4–0.6 moderate.

What if a class is never predicted?

Its precision is undefined and shown as a dash. The averages leave it out rather than counting it as 0.

Can I use it for two classes?

Yes, but the classification accuracy calculator gives more binary-specific measures, such as MCC and specificity.

Do I need class names?

No. Without a label line, classes are numbered in row order.

What about multi-label problems?

Those need one binary matrix per label. This page assumes each item has exactly one true class.

Sources

  • M. Sokolova and G. Lapalme, "A systematic analysis of performance measures for classification tasks", Information Processing & Management 45 (2009), doi:10.1016/j.ipm.2009.03.002.
  • J. Cohen, "A coefficient of agreement for nominal scales", Educational and Psychological Measurement 20 (1960), doi:10.1177/001316446002000104.

Related Tools