Confusion Matrix & Classification Metrics

Calculate accuracy, precision, recall, F1-score, specificity, balanced accuracy, and Matthews Correlation Coefficient (MCC) with worked formulas.

2×2 Confusion Matrix Inputs

Actual (+), Pred (+)
Actual (-), Pred (+) (Type I)
Actual (+), Pred (-) (Type II)
Actual (-), Pred (-)
Presets:

Type I vs. Type II Errors

• False Positive (Type I / FP): Rejecting null hypothesis when true (False alarm).
• False Negative (Type II / FN): Failing to reject null hypothesis when false (Missed detection).

Confusion Matrix Overview (N = 200)

Predicted Condition
Predicted Positive (PP)Predicted Negative (PN)
Actual TruthActual Positive (P)
95
True Positive (TP)
5
False Negative (FN)
Actual Negative (N)
5
False Positive (FP)
95
True Negative (TN)
AccuracyACC
95.00%
(TP + TN) / Total
PrecisionPPV
95.00%
TP / (TP + FP)
Recall (Sensitivity)TPR
95.00%
TP / (TP + FN)
SpecificityTNR
95.00%
TN / (TN + FP)
F1-ScoreF₁
0.9500
Harmonic mean P & R
Balanced AccuracyBA
95.00%
(Recall + Specificity) / 2
Matthews Corr. (MCC)MCC
0.9000
Scale: -1.0 to +1.0
PrevalencePREV
50.00%
Actual (+)/ Total

Extended Diagnostic Rates & Errors

MetricSymbolValueFormulaInterpretation
Negative Predictive ValueNPV95.00%TN / (TN + FN)Proportion of negative predictions that were truly negative.
False Positive Rate (Fall-out)FPR5.00%FP / (FP + TN) = 1 - SpecificityProportion of actual negative cases incorrectly flagged as positive (Type I error rate).
False Negative Rate (Miss Rate)FNR5.00%FN / (FN + TP) = 1 - RecallProportion of actual positive cases missed by the classifier (Type II error rate).
False Discovery RateFDR5.00%FP / (TP + FP) = 1 - PrecisionProportion of positive calls that are false alarms.
Diagnostic Odds RatioDOR361.00(TP × TN) / (FP × FN)Ratio of the odds of positive test in diseased vs. healthy individuals.

Deconstructing the 2×2 Confusion Matrix: Four Fundamental Quadrants

In statistical hypothesis testing and supervised machine learning, a confusion matrix(contingency table) cross-tabulates predicted algorithmic classifications against verified ground-truth observations. For binary classification, decisions map into four mutually exclusive quadrants:

True Positive (TP) • Accurate Detection

The positive condition was correctly flagged. In diagnostic oncology, an active malignant tumor is correctly identified, initiating timely clinical intervention.

False Positive (FP) • Type I Error (α)

The model raised a false alarm, flagging a negative case as positive. In financial fraud detection, a legitimate customer card purchase is falsely declined, causing friction.

False Negative (FN) • Type II Error (β)

The model missed an actual positive case. In cybersecurity intrusion detection, an active ransomware attack goes undetected, allowing lateral network infection.

True Negative (TN) • Accurate Rejection

The negative condition was correctly recognized. In automated email filtering, legitimate correspondence is properly routed to the primary inbox without alarm.

Comprehensive Classification Metrics Reference

From these four raw counts (where N = TP + FP +FN + TN), data scientists compute specialized diagnostic ratios to evaluate discrimination performance:

MetricFormulaRangeDiagnostic Interpretation
Accuracy (ACC)(TP + TN) / N0.0 to 1.0Overall proportion of correct classifications; distorted by class imbalance.
Precision (PPV)TP / (TP + FP)0.0 to 1.0Purity of positive alarms; critical when false positives incur heavy financial or human costs.
Recall / Sensitivity (TPR)TP / (TP + FN)0.0 to 1.0Coverage of actual positive cases; paramount in medical screening and threat detection.
Specificity (TNR)TN / (TN + FP)0.0 to 1.0Ability to identify true negatives; vital to avoid healthy patients undergoing invasive biopsies.
F1-Score (F₁)2·TP / (2·TP + FP + FN)0.0 to 1.0Harmonic mean of Precision and Recall; penalizes severe imbalance between the two.
Balanced Accuracy(TPR + TNR) / 20.0 to 1.0Average of Sensitivity and Specificity; immune to class imbalance inflation.
Matthews Corr. Coeff. (MCC)(TP·TN − FP·FN) / √((TP+FP)(TP+FN)(TN+FP)(TN+FN))−1.0 to +1.0Pearson correlation between ground truth and predictions; gold standard for imbalanced data.
Diagnostic Odds Ratio (DOR)(TP × TN) / (FP × FN)0.0 to ∞Measure of diagnostic test effectiveness independent of disease prevalence.

The Accuracy Paradox & Step-by-Step Worked Walkthrough

A widespread pitfall in data science is evaluating models trained on imbalanced datasets using raw Accuracy. Consider a payment gateway where only 0.1% of transactions are fraudulent (999 legitimate vs 1 fraud per 1,000). A trivial dummy classifier that unconditionally predicts "legitimate" for every transaction achieves 99.9% accuracywhile detecting exactly zero fraud cases! This distortion is known as theAccuracy Paradox.

Step-by-Step Worked Calculation Example

Consider a medical diagnostic test evaluated on N = 200 patients suspected of harboring a rare respiratory virus:

TP90
FP10
FN5
TN95
Accuracy: (90 + 95) / 200 = 185 / 200 = 92.50%
Precision: 90 / (90 + 10) = 90 / 100 = 90.00% (90% of positive test alarms have the virus)
Recall: 90 / (90 + 5) = 90 / 95 = 94.74% (test successfully catches 94.74% of sick patients)
Specificity: 95 / (95 + 10) = 95 / 105 = 90.48% (test correctly clears 90.48% of healthy patients)
F1-Score: (2 × 90) / (2 × 90 + 10 + 5) = 180 / 195 = 0.9231
MCC: [(90 × 95) − (10 × 5)] / √((90+10)(90+5)(95+10)(95+5))
  = (8,550 − 50) / √(100 × 95 × 105 × 100)
  = 8,500 / √(99,750,000) = 8,500 / 9,987.49 = 0.8511

Why MCC is superior to F1-Score: While F1-score ignores True Negatives completely, MCC incorporates all four quadrants symmetrically. An MCC score of +0.8511 indicates very strong positive correlation between prediction and ground truth, far superior to random guessing (0.0) or complete inverse classification (−1.0).

Practical Real-World Use Cases

  • Evaluate binary classifier performance: Input counts for True Positives, False Positives, True Negatives, and False Negatives to inspect accuracy, precision, recall, and F1-score simultaneously.
  • Tune classification thresholds for imbalanced data: Analyze precision and recall trade-offs when optimizing a fraud detection or medical diagnosis model where false negatives carry severe penalties.
  • Verify metric calculations for reports: Validate manual computations and formulas before submitting academic research papers, ML benchmarks, or stakeholder reports.
  • Calibrate multi-class classification buckets: Compute per-class sensitivity and specificity metrics to pinpoint which categories suffer from elevated confusion rates.

Absolute Privacy & Local Processing Guarantee

100% Client-side statistical evaluation. Machine learning validation datasets and medical test registries frequently contain confidential patient identifiers (HIPAA), proprietary financial transaction histories, or unreleased research.

OmniTools executes all matrix cross-tabulations, CSV pair parsing, division-by-zero guards, and LaTeX mathematical export exclusively in your browser's local memory using pure client-side TypeScript.

No prediction rows, labels, observations, or summary metrics are ever transmitted over a network connection, tracked via telemetry, or persisted on external cloud servers.

Frequently Asked Questions

How do you calculate Precision, Recall, F1-Score, and Accuracy from a confusion matrix?

Given True Positives (TP), False Positives (FP), False Negatives (FN), and True Negatives (TN): Precision = TP / (TP + FP), Recall (Sensitivity) = TP / (TP + FN), F1-Score = 2·(Precision·Recall)/(Precision + Recall), and Accuracy = (TP + TN) / Total.

Why is Matthews Correlation Coefficient (MCC) better than Accuracy on imbalanced datasets?

Standard accuracy can exceed 99% on an imbalanced dataset even if a model fails to detect the minority class entirely. MCC incorporates all four quadrants (TP, TN, FP, FN) into a balanced correlation score from −1 to +1.