induwara.lk
induwara.lkMachine learning · Metrics

Confusion Matrix Calculator — Accuracy, Precision, Recall, F1 & MCC

Enter the four counts of a binary confusion matrix (TP, FP, FN, TN) and read every standard classification metric — accuracy, precision, recall, specificity, F1, balanced accuracy, MCC and Cohen's kappa — each with the exact formula and your numbers substituted in. Matches scikit-learn, runs in your browser, no signup.

By Induwara AshinsanaUpdated Jul 8, 2026
Confusion matrix metrics

Predicted Positive, actually Positive.

Predicted Positive, actually Negative.

Predicted Negative, actually Positive.

Predicted Negative, actually Negative.

β>1 favours recall, β<1 favours precision.

Examples
Actual PositiveActual NegativeTotal
Predicted Positive90TP10FP100
Predicted Negative5FN895TN900
Total959051,000
Accuracy
0.9850
98.5%
F1 score
0.9231
92.31%
Balanced accuracy
0.9682
96.82%
MCC
0.9151
−1 … +1
MetricValue%
Accuracy0.985098.5%
Balanced accuracy0.968296.82%
Precision (PPV)0.900090%
Recall / Sensitivity (TPR)0.947494.74%
Specificity (TNR)0.989098.9%
Neg. predictive value (NPV)0.994499.44%
F1 score0.923192.31%
F1-score0.923192.31%
False positive rate (FPR)0.01101.1%
False negative rate (FNR)0.05265.26%
Prevalence0.09509.5%
Matthews correlation (MCC)0.9151
Cohen's kappa (κ)0.9148

MCC 0.9151Almost perfect agreement with the true labels.

Every metric is a closed-form function of TP, FP, FN and TN (scikit-learn / Wikipedia definitions). A metric with a zero denominator is shown as undefined, never a silent 0 or NaN. Sources are cited below the calculator.

How it works

A binary confusion matrix has four cells. True Positives (TP) and True Negatives (TN) are correct predictions; False Positives (FP) are Type I errors (a negative flagged as positive) and False Negatives (FN)are Type II errors (a positive that slipped through). Their sum is the sample size N. Every metric below is a closed-form function of these four integers, using the definitions in the scikit-learn model-evaluation documentation and the Wikipedia confusion-matrix table.

  • Accuracy = (TP + TN) / N
  • Precision (PPV) = TP / (TP + FP)
  • Recall / Sensitivity (TPR) = TP / (TP + FN)
  • Specificity (TNR) = TN / (TN + FP)
  • F1 = 2·TP / (2·TP + FP + FN)
  • Fβ = (1 + β²)·TP / ((1 + β²)·TP + β²·FN + FP)
  • Balanced accuracy = (Recall + Specificity) / 2
  • MCC = (TP·TN − FP·FN) / √((TP+FP)(TP+FN)(TN+FP)(TN+FN))
  • κ = (pₒ − pₑ) / (1 − pₑ), pₒ = Accuracy

The three summary scores earn their place by resisting the accuracy paradox. On a dataset that is 98% negative, a model that predicts "negative" for everything scores 0.98 accuracy while catching no positives at all. F1 collapses because recall is zero; balanced accuracy drops to 0.50 because it averages the two class-wise rates; MCC and Cohen's kappa fall near zero because both correct for the majority-class baseline — kappa by subtracting the chance agreement pₑ computed from the row and column totals, MCC by using all four cells as a correlation. When accuracy looks high but MCC is low, this tool shows a note so you weigh the right metric.

Two safeguards keep the numbers trustworthy. First, any metric whose denominator is zero is reported as undefinedrather than a silent 0 or NaN — precision needs TP + FP > 0, MCC needs every matrix margin non-zero. Second, F1 is computed two independent ways — from the raw counts and as the precision×recall harmonic mean — and the two are reconciled, which is what the "Matches scikit-learn" badge attests. Nothing you type is uploaded; the whole calculation happens in your browser.

Worked examples

Medical screening test

TP = 90, FP = 10, FN = 5, TN = 895 (N = 1000)

  1. Accuracy = (90 + 895) / 1000 = 0.9850
  2. Precision = 90 / (90 + 10) = 0.9000
  3. Recall = 90 / (90 + 5) = 0.9474
  4. Specificity = 895 / (895 + 10) = 0.9890
  5. F1 = 2·90 / (180 + 10 + 5) = 180 / 195 = 0.9231
  6. MCC = (90·895 − 10·5) / √(100·95·905·900) = 80500 / 87964.5 = 0.9151
  7. Cohen's κ: pₒ = 0.985, pₑ = (100·95 + 900·905)/1000² = 0.824 → κ = 0.161/0.176 = 0.9148

Spam filter (mild imbalance)

TP = 40, FP = 15, FN = 10, TN = 135 (N = 200)

  1. Accuracy = (40 + 135) / 200 = 0.8750
  2. Precision = 40 / (40 + 15) = 0.7273
  3. Recall = 40 / (40 + 10) = 0.8000
  4. Specificity = 135 / (135 + 15) = 0.9000
  5. F1 = 2·40 / (80 + 15 + 10) = 80 / 105 = 0.7619
  6. Balanced accuracy = (0.8000 + 0.9000) / 2 = 0.8500
  7. Cohen's κ: pₒ = 0.875, pₑ = (55·50 + 145·150)/200² = 0.6125 → κ = 0.2625/0.3875 = 0.6774

Accuracy paradox (heavy imbalance)

TP = 5, FP = 5, FN = 15, TN = 975 (N = 1000)

  1. Accuracy = (5 + 975) / 1000 = 0.9800 ← looks excellent
  2. Precision = 5 / (5 + 5) = 0.5000
  3. Recall = 5 / (5 + 15) = 0.2500 ← 75% of positives missed
  4. F1 = 2·5 / (10 + 5 + 15) = 10 / 30 = 0.3333
  5. MCC = (5·975 − 5·15) / √(10·20·980·990) = 4800 / 13929.8 = 0.3446
  6. Verdict: 0.98 accuracy hides a weak model — MCC 0.34 and F1 0.33 tell the truth.

Frequently asked questions

Sources & references

Related tools

Rate this tool
Be the first to rate

Comments & feedback

Spotted a bug or want an improvement? Tell us — our team reviews every comment, and good ideas get built. Comments are public and anonymous.

Found a bug, edge case, or want to suggest an improvement?

Email me at [email protected] — most fixes ship within 24 hours.