TP

Confusion Matrix Calculator

Accuracy, precision (PPV), recall (sensitivity), specificity, NPV, F1 and Fβ, balanced accuracy, MCC and prevalence from the true and false positives and negatives of a diagnostic test or a classifier.

Statistics & ProbabilityRuns in your browser

Result…

Show

The same counts, named the way machine learning or medicine usually names them. The working lists every measure.

Positive cases correctly flagged positive.

Negative cases wrongly flagged positive.

Positive cases missed (flagged negative).

Negative cases correctly flagged negative.

β > 1 weighs recall more, β < 1 weighs precision more; β = 1 gives F1.

 

…

How it's calculated

Accuracy = (TP + TN) ÷ (TP + FP + FN + TN)
Precision (PPV) = TP ÷ (TP + FP); NPV = TN ÷ (TN + FN)
Recall (sensitivity) = TP ÷ (TP + FN); specificity = TN ÷ (TN + FP)
F1 = 2TP ÷ (2TP + FP + FN); Fβ = (1 + β²)TP ÷ ((1 + β²)TP + β²FN + FP)
Balanced accuracy = (sensitivity + specificity) ÷ 2
MCC = (TP × TN − FP × FN) ÷ √((TP + FP)(TP + FN)(TN + FP)(TN + FN))

A confusion matrix counts how a yes-or-no test or classifier did against the truth: true positives and true negatives are right, false positives and false negatives are wrong. Every common measure comes from these four counts. Medicine usually talks about sensitivity, specificity and the predictive values; machine learning about precision, recall and the F1 score, which are the same ratios under other names. Accuracy can mislead when one class is rare, so the page also gives balanced accuracy and the Matthews correlation coefficient (MCC), which stay honest on unbalanced data. When a ratio has nothing to divide by, such as precision with no positive predictions, it says so instead of showing a number.

Your numbers, step by step

    About the Confusion Matrix calculator

    This confusion matrix calculator turns four counts, true positives, false positives, false negatives and true negatives, into every common measure of how well a yes-or-no test performs. Machine learning practitioners get accuracy, precision, recall, the F1 score, an Fβ score with the β they choose, balanced accuracy and the Matthews correlation coefficient (MCC). Switch to the diagnostic test view for the names used in medicine and screening: sensitivity, specificity, positive and negative predictive value (PPV and NPV) and prevalence. They are the same ratios, so the working lists all of them with your numbers. When a ratio has nothing to divide by, such as precision when nothing was predicted positive, the calculator says it is not defined rather than inventing a value. To see how prevalence changes what a positive result means, try the Bayes' theorem calculator.

    How to use it

    1. Choose “Classifier metrics” or “Diagnostic test metrics” under “Show”.
    2. Enter the “True positives (TP)”, “False positives (FP)”, “False negatives (FN)” and “True negatives (TN)” as whole numbers.
    3. For classifiers, set the “β for the Fβ score”: 2 weighs recall more, 0.5 weighs precision more.
    4. Read the accuracy and the other measures, and open the steps to see every ratio worked out.

    Worked example

    A model is tested on 1,000 cases: 80 true positives, 30 false positives, 20 false negatives and 870 true negatives. Accuracy is (80 + 870) ÷ 1,000 = 95%. Precision is 80 ÷ 110 = 72.73% and recall is 80 ÷ 100 = 80%, so F1 = 160 ÷ 210 = 76.19% and the F2 score is 78.43%. Specificity is 870 ÷ 900 = 96.67%, NPV is 870 ÷ 890 = 97.75%, balanced accuracy is 88.33%, prevalence is 10% and the MCC is 0.7351.

    Updated

    Questions people ask

    How do I calculate precision and recall from a confusion matrix?

    Precision = TP ÷ (TP + FP), the share of positive predictions that were right. Recall = TP ÷ (TP + FN), the share of actual positives that were found.

    Are sensitivity and recall the same thing?

    Yes. Sensitivity in medicine and recall in machine learning are both TP ÷ (TP + FN). Likewise, positive predictive value (PPV) is the same as precision.

    Why can accuracy be misleading?

    When one class is rare, a test that always says negative can score high accuracy while finding nothing. Balanced accuracy and the MCC take both classes into account, so they expose that problem.

    What is the formula for the Matthews correlation coefficient?

    MCC = (TP × TN − FP × FN) ÷ √((TP + FP)(TP + FN)(TN + FP)(TN + FN)). It runs from −1 to 1, where 0 is no better than chance; the phi coefficient article on Wikipedia explains the case where a margin is zero.

    What is a common mistake with predictive values?

    Assuming a test's PPV carries over to another group. PPV and NPV depend on prevalence, so the same test gives a lower PPV where the condition is rarer, while sensitivity and specificity stay the same.

    Can a confusion matrix tell me what my own test result means?

    It only describes the counts you enter. A single result depends on your own risk and the test used, so ask a doctor or other clinician to interpret it.

    More in Statistics & Probability