Accuracy Calculator

Calculate accuracy, precision, recall, specificity, and F1 score from a confusion matrix — with a class imbalance warning built in.

Enter your confusion matrix — true positives, false positives, true negatives, and false negatives — and this calculator works out accuracy, precision, recall, specificity, F1 score, and more, with a warning if your data looks imbalanced enough that accuracy alone could be misleading.

Enter your confusion matrix Probability & classification
Predicted Positive
Predicted Negative
Actual Positive
True Positives (TP)
False Negatives (FN)
Actual Negative
False Positives (FP)
True Negatives (TN)

What is a confusion matrix?

Whenever a test, model, or diagnostic tool makes a yes/no prediction, its results can be sorted into exactly four categories — and organizing those four categories into a simple 2×2 grid, called a confusion matrix, is the foundation for essentially every classification accuracy metric that follows.

CategoryMeaning
True Positive (TP)Predicted positive, actually positive — a correct positive call
False Positive (FP)Predicted positive, actually negative — a false alarm
True Negative (TN)Predicted negative, actually negative — a correct negative call
False Negative (FN)Predicted negative, actually positive — a missed detection

Every one of the metrics covered in this article — accuracy, precision, recall, specificity, F1 score — is simply a different ratio built from these same four numbers. Once you have an accurate confusion matrix for a test or model, every other metric follows directly from it.

How accuracy is calculated

Accuracy formula Accuracy = (True Positives + True Negatives) ÷ Total observations

Worked example — TP=85, FP=10, TN=90, FN=15:

  1. Total observations: 85 + 10 + 90 + 15 = 200
  2. Correct predictions: 85 + 90 = 175
  3. Accuracy: 175 ÷ 200 = 87.5%

Accuracy answers the most intuitive question: out of everything the test or model evaluated, what fraction did it get right? It’s the most familiar classification metric, but — as covered below — it isn’t always the most informative one, particularly when the two classes being predicted aren’t roughly equal in size.

Precision and recall

Precision (Positive Predictive Value) Precision = True Positives ÷ (True Positives + False Positives)

Precision answers: of everything the test called positive, how much was actually positive? A high-precision test has few false alarms — when it says positive, it’s usually right.

Recall (Sensitivity) Recall = True Positives ÷ (True Positives + False Negatives)

Recall answers a different question: of everything that’s actually positive, how much did the test successfully catch? A high-recall test misses few actual positives — it’s good at finding what it’s looking for, even if that sometimes means more false alarms along the way.

Using the same example (TP=85, FP=10, TN=90, FN=15):

  1. Precision: 85 ÷ (85 + 10) = 85 ÷ 95 ≈ 89.5%
  2. Recall: 85 ÷ (85 + 15) = 85 ÷ 100 = 85.0%

Precision and recall exist in genuine tension with each other in most real systems — making a test more sensitive (catching more true positives, raising recall) typically also catches more false positives along the way (lowering precision), and vice versa. Which one matters more depends entirely on the specific application and the relative cost of each type of error.

The accuracy paradox and class imbalance

Accuracy can be seriously misleading when the two classes being predicted aren’t roughly balanced in size — a situation common enough in real-world data that it has its own name: the accuracy paradox.

Illustrative example: imagine a rare disease that affects only 1% of a population. A test that simply predicts “negative” for every single person, without doing any actual diagnostic work at all, would be 99% accurate — since 99% of people genuinely don’t have the disease. Despite that impressive-sounding accuracy figure, this test is completely useless, since it has 0% recall — it never catches a single actual case of the disease it’s supposed to detect.

This is exactly why precision, recall, and F1 score matter alongside (and often more than) raw accuracy whenever the underlying classes are imbalanced. This calculator flags a prevalence-based imbalance warning specifically for this reason — a high or low prevalence figure is a signal to look past the headline accuracy number and check precision, recall, and F1 more carefully before concluding a test or model is genuinely useful.

This same paradox appears throughout real-world statistics wherever the outcome being measured is naturally rare — fraud detection (most transactions are legitimate), rare disease screening (most people don’t have the condition), and manufacturing defect detection (most units pass inspection) all share this same underlying structure. In every one of these cases, a naive “always predict the common outcome” approach achieves deceptively high accuracy while providing zero practical value, which is precisely the scenario the accuracy paradox describes.

MetricFormulaAnswers
SpecificityTN ÷ (TN + FP)Of actual negatives, how many were correctly identified?
Negative Predictive ValueTN ÷ (TN + FN)Of everything called negative, how much was actually negative?
False Positive RateFP ÷ (FP + TN)Of actual negatives, how many were incorrectly flagged positive?
False Negative RateFN ÷ (FN + TP)Of actual positives, how many were missed?

Specificity is recall’s mirror image, applied to the negative class instead of the positive class — it’s especially important in medical testing contexts, where a highly specific test rarely produces a false alarm for a healthy patient. False positive rate is simply the complement of specificity (they always add up to 100%), and false negative rate is the complement of recall in the same way — these paired relationships are useful to keep in mind, since a metric and its complement always sum to a full 100% by definition.

F1 score: balancing precision and recall

F1 score formula F1 = 2 × (Precision × Recall) ÷ (Precision + Recall)

F1 score is the harmonic mean of precision and recall, specifically chosen (rather than a simple average) because the harmonic mean penalizes a large imbalance between the two more heavily than a simple average would. A test with 100% precision and 10% recall has a simple average of 55%, which sounds reasonably solid — but its F1 score is only about 18%, correctly reflecting that a test which misses 90% of actual positives isn’t a genuinely balanced or useful performer, regardless of how precise its few positive calls are.

F1 score is most useful specifically when precision and recall matter roughly equally for a given application and you want one single number that captures both — for situations where one clearly matters more than the other (a screening test where missing a case is far worse than a false alarm, for instance), looking at recall and precision separately, rather than blending them into F1, usually gives a clearer picture of what actually matters for the decision at hand.

F-beta scores generalize F1 for situations where precision and recall genuinely shouldn’t be weighted equally. An F2 score weights recall twice as heavily as precision, appropriate for a screening context where missing a positive case is considered substantially worse than a false alarm (many medical screening applications favor this weighting). An F0.5 score does the reverse, weighting precision more heavily — appropriate when false alarms are the more costly error, such as a spam filter where incorrectly flagging legitimate email as spam is often considered worse than letting an occasional spam message through. F1 itself is simply the special case where both are weighted equally.

Real-world applications

Medical diagnostic testing is one of the most consequential real-world applications of these exact metrics — sensitivity (recall) and specificity are the standard statistics used to evaluate any diagnostic test, from a simple rapid test to a complex imaging-based diagnosis, and understanding the tradeoff between them is central to interpreting what a positive or negative test result actually means for an individual patient.

Machine learning model evaluation uses this identical framework for classification models of every kind — spam filters, fraud detection systems, image recognition, and countless other applications all report precision, recall, and F1 score (often alongside or instead of raw accuracy) specifically because of the accuracy paradox described above, particularly in domains like fraud detection where the “positive” class (actual fraud) is naturally rare.

Quality control and manufacturing inspection systems use the same confusion matrix framework to evaluate automated defect detection — a system with high recall catches nearly all actual defects (important for safety-critical products) while a system with high precision rarely flags a good product as defective (important for manufacturing efficiency), and the right balance depends on the specific cost of a missed defect versus a false alarm in that particular production context.

Search and information retrieval — the results a search engine or database query returns — is where the terms “precision” and “recall” actually originated historically, before being adopted broadly across statistics and machine learning. A search with high precision returns mostly relevant results with few irrelevant ones mixed in; a search with high recall successfully surfaces most of the truly relevant results that exist, even if that means including some irrelevant ones along the way. Most modern search systems are tuned with an explicit precision/recall tradeoff in mind, often prioritizing precision heavily for the first page of results a user actually sees.

Common mistakes to avoid

  • Reporting accuracy alone without checking class balance. A high accuracy figure on an imbalanced dataset can hide a genuinely poor-performing test or model — always check precision, recall, and prevalence together, not accuracy in isolation.
  • Confusing precision and recall. These answer different questions (how trustworthy are positive calls, vs. how many actual positives were caught) and are easy to mix up — always double-check which one a specific application actually needs to prioritize.
  • Averaging precision and recall instead of using F1’s harmonic mean. A simple average can mask a severe imbalance between the two — F1’s harmonic mean is specifically designed to penalize that imbalance more realistically.
  • Ignoring specificity in favor of sensitivity alone. Both matter, especially in medical and diagnostic contexts — a highly sensitive test with poor specificity produces many false alarms, which has its own real costs (unnecessary follow-up testing, patient anxiety, resource use).
  • Treating a confusion matrix built from a small sample as definitive. Metrics calculated from a small number of observations carry substantial statistical uncertainty — a confusion matrix built from just a few dozen cases should be interpreted with real caution compared to one built from a much larger sample.
  • Assuming F1 score is always the right single metric to optimize. F1 treats precision and recall as equally important, which isn’t true for every application — when one type of error is clearly more costly than the other, optimizing precision or recall directly (or using a weighted F-beta score) usually makes more sense than defaulting to standard F1.
  • Forgetting that specificity and false positive rate are complements. These two metrics always sum to exactly 100% by definition — if you already know one, you automatically know the other, and reporting both as if they were independent pieces of information is redundant.
  • Building a confusion matrix from a non-representative sample. If the sample used to build the matrix doesn’t reflect the real-world population a test or model will actually be used on — particularly regarding class balance — the resulting metrics won’t accurately predict real-world performance.
Frequently asked questions
How do you calculate accuracy from a confusion matrix?
Accuracy = (True Positives + True Negatives) ÷ Total observations. For example, with TP=85, FP=10, TN=90, FN=15: total = 200, correct = 85+90 = 175, accuracy = 175÷200 = 87.5%. This calculator computes accuracy along with precision, recall, specificity, and F1 score from the same four inputs.
What is the difference between precision and recall?
Precision = TP ÷ (TP + FP), answering 'of everything predicted positive, how much was actually positive?' Recall = TP ÷ (TP + FN), answering 'of everything actually positive, how much did we catch?' They exist in tension — improving one often reduces the other, and which matters more depends on the specific application.
Why is accuracy sometimes a misleading metric?
This is called the accuracy paradox. If one class is rare (say, 1% of cases), a model that always predicts the common class scores 99% accuracy while being completely useless — it has 0% recall for the rare class. This is why precision, recall, and F1 score matter alongside accuracy, especially for imbalanced data.
How do you calculate F1 score?
F1 = 2 × (Precision × Recall) ÷ (Precision + Recall) — the harmonic mean of precision and recall. It's used instead of a simple average because the harmonic mean penalizes a large imbalance between the two more heavily, giving a more realistic single-number summary when both matter roughly equally.
What is specificity and how is it different from precision?
Specificity = TN ÷ (TN + FP), measuring how well a test identifies actual negatives correctly. Precision = TP ÷ (TP + FP), measuring how trustworthy positive predictions are. Specificity focuses on the negative class; precision focuses on the positive class — both are useful, especially together in medical testing contexts.
What sample size do I need for a reliable confusion matrix?
There's no single fixed number, but metrics from a small sample (a few dozen cases or fewer) carry substantial statistical uncertainty and should be interpreted cautiously. Larger samples produce more stable, reliable accuracy, precision, and recall figures — always consider sample size alongside the metrics themselves, not just the metrics in isolation.