Confusion matrix calculator

Paste the confusion matrix of a classification model, actual classes in rows and predicted classes in columns, to get the standard metrics in one go: accuracy, precision, recall and F1 for every class, their macro and weighted averages, the Matthews correlation coefficient and Cohen's kappa.

Rows = actual class, columns = predicted class. Rows separated by semicolons, counts by spaces or commas: 40 10; 5 45.
Optional, in the order of the rows, separated by spaces or commas. Empty: classes are numbered.
Accuracy0.873333
Macro F10.871851
Matthews correlation coefficient (MCC)
0.810043
Cohen's kappa
0.809556
Macro precision
0.87144
Macro recall
0.873401
Weighted F1 (by class size)
0.872764
Precision per class
A: 0.909091 B: 0.851064 C: 0.854167
Recall per class
A: 0.909091 B: 0.8 C: 0.911111
F1 per class
A: 0.909091 B: 0.824742 C: 0.88172
Actual cases per class (support)
A: 55 B: 50 C: 45
Total cases
150

Runs in your browser: nothing you enter is sent anywhere.

How to use it

  1. Paste the matrix with one row per actual class: 50 3 2; 5 40 5; 0 4 41.
  2. Optionally add class names in the same order, such as cat dog bird.
  3. Read accuracy and macro F1 at the top, the other overall scores below, and the per-class precision, recall, F1 and support.

Formulas

For a class c with TP on the diagonal, FP the rest of its column and FN the rest of its row:

  • Precision = TP/(TP + FP), recall = TP/(TP + FN), F1 = 2TP/(2TP + FP + FN)
  • Accuracy = trace / total
  • Macro average = mean over classes; weighted average = mean weighted by support
  • MCC = (c·s − Σ pₖtₖ) / √((s² − Σ pₖ²)(s² − Σ tₖ²)) with c the trace, s the total, tₖ the row sums and pₖ the column sums
  • Cohen’s κ = (p₀ − pₑ)/(1 − pₑ) with p₀ = accuracy and pₑ = Σ pₖtₖ / s²

Worked example

A spam filter on 100 emails: actual spam predicted spam 40, spam predicted ham 10, ham predicted spam 5, ham predicted ham 45, so the matrix is 40 10; 5 45.

  • Accuracy = 85/100 = 0.85.
  • Spam: precision 40/45 = 0.888889, recall 40/50 = 0.8, F1 80/95 = 0.842105.
  • Ham: precision 45/55 = 0.818182, recall 0.9, F1 90/105 = 0.857143.
  • Macro F1 = 0.849624. MCC = (40·45 − 5·10)/√(45·55·50·50) = 0.703526, and κ = (0.85 − 0.5)/0.5 = 0.7.

Tips and common mistakes

  • Check the orientation: swapping rows and columns swaps precision and recall.
  • Accuracy alone is misleading with imbalanced classes; look at recall of the small classes, macro F1 or MCC.
  • For a medical-style 2×2 table with sensitivity, specificity and predictive values, use the sensitivity and specificity calculator.

Frequently asked questions

What is the difference between precision and recall?

Precision is the share of predictions of a class that are correct: TP/(TP + FP), a column of the matrix. Recall is the share of actual cases of the class that were found: TP/(TP + FN), a row. F1 is their harmonic mean, 2PR/(P + R).

Macro or weighted average?

The macro average gives every class the same weight, so small classes count as much as large ones. The weighted average weights each class by its number of actual cases and stays close to accuracy when classes are imbalanced.

Why is MCC recommended for imbalanced data?

The Matthews correlation coefficient uses all four kinds of outcomes and is high only when the model does well on every class. A model that always predicts the majority class gets MCC 0 even if its accuracy looks high.

Which way round should the matrix be?

This calculator expects actual classes in rows and predicted classes in columns, as scikit-learn prints it. If your table is the other way round, precision and recall swap.

More in Math

All tools in this category: Math ·Updated November 4, 2026

Privacy choices

Google Analytics shows us which tools are used and whether they work. It may set cookies. Your choice is kept in this browser.