Saved

Press the star next to a tool's name to save it.

All my tools

Confusion Matrix Calculator

Use this confusion matrix calculator to find accuracy, precision, recall, specificity, F1 score, balanced accuracy and MCC from the true and false positive and negative counts.

Restored your last values ·

Enter the four counts of a two-class confusion matrix: true positives, false positives, false negatives and true negatives. You get accuracy, precision, recall, specificity, the F1 score, balanced accuracy, the Matthews correlation coefficient and the other usual measures.

Everything is worked out in your browser. Nothing you type is sent anywhere.

–

–

Enter the four counts of your confusion matrix, then press Calculate.

See full result

A confusion matrix calculator turns the four counts of a classifier or a medical test into all the usual measures of how well it works. Enter the true positives, false positives, false negatives and true negatives, and you get accuracy, precision, recall, specificity, the F1 score, balanced accuracy, the Matthews correlation coefficient, the false positive and false negative rates and more. It is used for machine learning models, diagnostic tests and screening. It runs in your browser.

How to use the confusion matrix calculator

  1. Enter the true positives (TP) and false positives (FP).
  2. Enter the false negatives (FN) and true negatives (TN).
  3. Press Calculate.

The matrix and its measures

The Wikipedia article on the confusion matrix lays out the counts and the measures that come from them. Recall, also called sensitivity or the true positive rate, is TP divided by the number of real positives, TP + FN. Specificity, the true negative rate, is TN divided by the number of real negatives, TN + FP. Precision, the positive predictive value, is TP ÷ (TP + FP), and the negative predictive value is TN ÷ (TN + FN). The false positive rate is FP ÷ (FP + TN), the false negative rate is FN ÷ (TP + FN), and the prevalence is the share of real positives in all cases.[1] Accuracy is (TP + TN) ÷ all cases, and the F1 score is 2 × precision × recall ÷ (precision + recall), which equals 2TP ÷ (2TP + FP + FN).[1]

More measures

Balanced accuracy is the average of recall and specificity. The Matthews correlation coefficient takes all four counts into account; in the form (TP × TN − FP × FN) ÷ √((TP + FP)(TP + FN)(TN + FP)(TN + FN)) it ranges from −1 to +1, and it is equal to the square root of recall, specificity, precision and negative predictive value multiplied together, minus the square root of the four corresponding error rates multiplied together, which is how the page writes it.[1] The page also lists the false discovery rate, FP ÷ (TP + FP), and the false omission rate, FN ÷ (TN + FN), and the likelihood ratios, which are recall ÷ false positive rate for a positive result and false negative rate ÷ specificity for a negative one.[2] The calculator shows them all.

A screening example

The article on sensitivity and specificity works through a screening test with a sensitivity of 67 percent and a specificity of 91 percent, applied to 2,030 people in which the prevalence is 1.48 percent. The counts are 20 true positives, 10 false negatives, 180 false positives and 1,820 true negatives. The page gives an accuracy of about 90.64 percent, an F1 score of about 0.174, a positive predictive value of 10 percent, a false discovery rate of 90 percent, a negative predictive value of about 99.45 percent, and likelihood ratios of about 7.41 and 0.366.[2] Enter the four counts to see the same results. Although the test finds two thirds of cases, nine in ten of its positives are false, because the condition is rare.

A small classifier example

The confusion matrix page has an example of 12 people, 8 with a cancer and 4 without. The classifier makes 6 true positives, 2 false negatives, 1 false positive and 3 true negatives.[1] The page does not give the measures, and we worked them out: the accuracy is 9 ÷ 12 = 75 percent, the recall is 75 percent, the specificity is 75 percent, the precision is 6 ÷ 7 = 85.7 percent, and the F1 score is 0.8.

When accuracy misleads

The page warns that accuracy gives misleading results if the data set is unbalanced. Its example has 95 cancer samples and 5 non-cancer samples, and a classifier that labels every sample as cancer. Its accuracy is 95 percent and its recall for cancer is 100 percent, yet it finds none of the non-cancer cases. The page notes that the F1 score would be over 97.4 percent and is even more unreliable in such cases, and that the Matthews coefficient has been called the most informative single measure for a confusion matrix.[1] Enter 95, 5, 0 and 0 to see the high accuracy and F1, with a specificity of 0 and balanced accuracy of 50 percent, and with the Matthews coefficient not defined.

Prevalence and predictive values

The sensitivity page points out that the predictive values, but not sensitivity and specificity, are affected by prevalence.[2] The same test gives a very different precision in a group where the condition is common and one where it is rare. If you apply a model to data with a different class balance from the data on which you tested it, recalculate the predictive values from the recall, the specificity and the new prevalence.

Choosing a measure

Use recall when missing a positive is costly, as in screening for a serious illness. Use precision when false alarms are costly, as in spam filtering that might delete real mail. The F1 score balances the two for the positive class. Specificity matters when the negative class is large. For a single summary on unbalanced data, balanced accuracy or the Matthews coefficient is safer than accuracy.

Common mistakes

  • Mixing up the rows and columns. Real cases and predicted cases are different axes.
  • Trusting accuracy on rare classes. A model that always says no can be 99 percent accurate.
  • Confusing precision with recall. They divide by different totals.
  • Quoting predictive values without the prevalence. They depend on it.

What next

Use this often? Add Confusion Ma to your Home screen.

Frequently asked questions

What is a confusion matrix calculator?

A tool that turns the counts of true and false positives and negatives into accuracy, precision, recall, specificity, F1 and other measures.

How is the F1 score calculated?

F1 = 2 × precision × recall ÷ (precision + recall), which equals 2TP ÷ (2TP + FP + FN).

What is the difference between sensitivity and specificity?

Sensitivity is the share of real positives that are found, TP ÷ (TP + FN). Specificity is the share of real negatives that are found, TN ÷ (TN + FP).

Why can accuracy be misleading?

On unbalanced data a model that always predicts the common class has high accuracy and finds none of the rare class.

What is the Matthews correlation coefficient?

A balanced measure from −1 to +1 that uses all four counts. It is 1 for a perfect classifier and 0 for random guessing.

What went wrong?

We only receive the reason you pick, never what you typed into the tool.

Thanks for your feedback.

More Math Tools