How to use it
- Paste the matrix with one row per actual class:
50 3 2; 5 40 5; 0 4 41. - Optionally add class names in the same order, such as
cat dog bird. - 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.