Confusion Matrix Calculator

A Confusion Matrix Calculator helps you evaluate a classification model by calculating accuracy, precision, recall, F1 score, specificity, and other performance metrics from four values: true positives, true negatives, false positives, and false negatives. These results show how often a model makes correct predictions and which types of mistakes it makes.

Whether you are studying machine learning, testing a classification algorithm, or analyzing research data, the right metrics help you judge model performance more clearly. This guide explains the confusion matrix formulas, shows how to use the calculator, and walks through a verified numerical example.

Key Takeaways

  • A confusion matrix compares a model’s predictions with the actual class labels.
  • True positives, true negatives, false positives, and false negatives form the basis of binary classification metrics.
  • Accuracy measures overall correctness, precision evaluates positive predictions, and recall measures how many actual positive cases are detected.
  • The F1 score combines precision and recall into one measure.
  • High accuracy does not always mean a model performs well, particularly when the dataset contains very different numbers of examples in each class.

What Does a Confusion Matrix Calculator Do?

A Confusion Matrix Calculator uses classification results to calculate numerical measures of a model’s performance. You enter the four outcome counts, and the calculator determines how the model performed on the evaluated dataset.

The calculator is useful for binary classification tasks, such as identifying spam emails, detecting fraudulent transactions, or classifying medical test results. It can help you identify whether a model misses positive cases or produces too many false alarms. The results are based on the supplied counts. They do not independently verify the dataset, the quality of the model, or whether the chosen evaluation data represent real-world conditions.

What Are the Four Parts of a Confusion Matrix?

A binary confusion matrix contains four possible outcomes. Each outcome describes whether the actual class and the predicted class agree.

OutcomeMeaningExample: Spam detection
True Positive (TP)The model correctly predicts a positive case.A spam email is correctly identified as spam.
True Negative (TN)The model correctly predicts a negative case.A legitimate email is correctly identified as legitimate.
False Positive (FP)The model predicts positive when the actual class is negative.A legitimate email is incorrectly marked as spam.
False Negative (FN)The model predicts negative when the actual class is positive.A spam email is incorrectly marked as legitimate.

True positives and true negatives are correct predictions. False positives and false negatives are classification errors.

How Is a Binary Confusion Matrix Arranged?

A common arrangement places actual classes in rows and predicted classes in columns.

Actual classPredicted positivePredicted negative
Actual positiveTPFN
Actual negativeFPTN

The arrangement matters when reading a matrix produced by software. Some tools use a different display convention, so check the axis labels before entering values into a calculator.

The total number of evaluated observations is the sum of all four outcomes.

\[ N = TP + TN + FP + FN \]

Here, N represents the total number of evaluated observations.

Confusion Matrix Formulas

The four outcome counts are used to calculate several classification metrics. Each metric answers a different question about model performance.

1. Accuracy

Accuracy measures the proportion of all predictions that are correct.

\[ \text{Accuracy} = \frac{TP + TN}{TP + TN + FP + FN} \]

A result of 0.86 corresponds to 86% accuracy. Accuracy can provide a useful overall measure when the classes are reasonably balanced and the costs of different errors are similar.

2. Precision

Precision measures the proportion of predicted positive cases that are actually positive.

\[ \text{Precision} = \frac{TP}{TP + FP} \]

High precision means that positive predictions are usually correct. It is useful when false positives create significant costs, such as unnecessary fraud investigations or false spam alerts.

3. Recall (Sensitivity)

Recall, also called sensitivity or the true positive rate, measures the proportion of actual positive cases correctly identified by the model.

\[ \text{Recall} = \frac{TP}{TP + FN} \]

High recall means the model misses fewer actual positive cases. It can be particularly useful when missing a positive case carries a high cost.

4. Specificity

Specificity, also called the true negative rate, measures the proportion of actual negative cases correctly identified.

\[ \text{Specificity} = \frac{TN}{TN + FP} \]

High specificity means the model correctly recognizes negative cases and produces fewer false positives among them.

5. F1 Score

The F1 score combines precision and recall using their harmonic mean. It gives a single score that reflects both measures.

\[ F1 = 2 \times \frac{\text{Precision} \times \text{Recall}}{\text{Precision} + \text{Recall}} \]

An equivalent formula uses the four confusion matrix counts directly.

\[ F1 = \frac{2 \times TP}{2 \times TP + FP + FN} \]

The F1 score is useful when both precision and recall matter. It does not include true negatives directly, so it should not be treated as a complete measure of performance in every classification task.

6. False Positive Rate

The false positive rate (FPR) measures the proportion of actual negative cases incorrectly classified as positive.

\[ FPR = \frac{FP}{FP + TN} \]

A lower FPR means fewer false alarms among actual negative cases.

7. False Negative Rate

The false negative rate (FNR) measures the proportion of actual positive cases incorrectly classified as negative.

\[ FNR = \frac{FN}{FN + TP} \]

A lower FNR means fewer actual positive cases are missed.

8. Negative Predictive Value

The negative predictive value (NPV) measures the proportion of negative predictions that are actually negative.

\[ NPV = \frac{TN}{TN + FN} \]

NPV is useful when you need to assess how often a negative result can be trusted. Its value can depend on how common the positive class is in the evaluated population.

9. False Discovery Rate

The false discovery rate (FDR) measures the proportion of predicted positive cases that are false positives.

\[ FDR = \frac{FP}{FP + TP} \]

For a binary confusion matrix with a defined positive-prediction total, precision and FDR add up to 1.

\[ \text{Precision} + FDR = 1 \]

10. Matthews Correlation Coefficient

The Matthews correlation coefficient (MCC) measures the agreement between actual and predicted binary classifications using all four confusion matrix counts.

\[ MCC = \frac{TP \times TN – FP \times FN}{\sqrt{(TP + FP) \times (TP + FN) \times (TN + FP) \times (TN + FN)}} \]

MCC ranges from −1 to +1 when the denominator is defined.

  • A value of +1 indicates perfect agreement.
  • A value of 0 indicates no correlation under this measure.
  • A value of −1 indicates complete disagreement.

MCC can be useful when class sizes differ, since it incorporates true positives, true negatives, false positives, and false negatives.

How to Use the Confusion Matrix Calculator

Follow these steps to calculate your classification metrics.

  1. Enter the true positive (TP) count. This is the number of positive cases the model correctly identifies.
  2. Enter the true negative (TN) count. This is the number of negative cases the model correctly identifies.
  3. Enter the false positive (FP) count. This is the number of negative cases incorrectly classified as positive.
  4. Enter the false negative (FN) count. This is the number of positive cases incorrectly classified as negative.
  5. Review the calculated results. Check accuracy, precision, recall, F1 score, and the other available metrics.

Use nonnegative counts from the same evaluation dataset. Each count must refer to the same classification task and positive-class definition.

If your data come from a multiclass model, check whether the calculator accepts a separate binary confusion matrix for each class. A single set of four counts cannot represent every relationship in a multiclass confusion matrix.

Confusion Matrix Calculator Example

Suppose a spam detection model evaluates 100 emails. Its predictions produce the following results:

OutcomeCount
True positives (TP)42
True negatives (TN)44
False positives (FP)6
False negatives (FN)8
Total100

These values mean the model correctly identifies 42 spam emails and 44 legitimate emails. It incorrectly marks 6 legitimate emails as spam and misses 8 spam emails.

Step 1: Calculate Accuracy

\[ \text{Accuracy} = \frac{42 + 44}{42 + 44 + 6 + 8} \] \[ \text{Accuracy} = \frac{86}{100} = 0.86 = 86\% \]

The model classifies 86% of the evaluated emails correctly.

Step 2: Calculate Precision

\[ \text{Precision} = \frac{42}{42 + 6} \] \[ \text{Precision} = \frac{42}{48} = 0.875 = 87.5\% \]

Of all emails predicted to be spam, 87.5% are actually spam.

Step 3: Calculate Recall

\[ \text{Recall} = \frac{42}{42 + 8} \] \[ \text{Recall} = \frac{42}{50} = 0.84 = 84\% \]

The model detects 84% of all actual spam emails.

Step 4: Calculate Specificity

\[ \text{Specificity} = \frac{44}{44 + 6} \] \[ \text{Specificity} = \frac{44}{50} = 0.88 = 88\% \]

The model correctly identifies 88% of the legitimate emails.

Step 5: Calculate the F1 Score

\[ F1 = 2 \times \frac{0.875 \times 0.84}{0.875 + 0.84} \] \[ F1 \approx 0.8571 = 85.71\% \]

The F1 score summarizes the balance between precision and recall.

Step 6: Review the Other Metrics

Using the same four counts, the remaining metrics are:

MetricResult
False positive rate12%
False negative rate16%
Negative predictive value84.62%
False discovery rate12.5%
Matthews correlation coefficientApproximately 0.735

The results show that the model correctly classifies most emails, but it still misses some spam and incorrectly flags some legitimate messages. Whether that performance is acceptable depends on the cost of each type of error.

How Do You Interpret Confusion Matrix Results?

The best metric depends on the problem you are trying to solve. A high score alone does not establish that a classification model is suitable for its intended use.

MetricWhat a higher value indicatesWhen it is useful
AccuracyMore predictions are correct overall.Assessing overall performance when class balance and error costs support its use.
PrecisionPositive predictions are more often correct.Reducing false alarms and unnecessary follow-up work.
RecallMore actual positive cases are detected.Reducing missed positive cases.
SpecificityMore actual negative cases are identified correctly.Limiting false positives among negative cases.
F1 scorePrecision and recall are balanced at a higher level.Comparing models when both measures matter.
MCCStronger agreement between actual and predicted labels.Evaluating binary classifications using all four outcome counts.

For example, a model used to detect a rare condition may achieve high accuracy by predicting the negative class for almost every case. Its recall could still be poor. Google’s Machine Learning Crash Course explains this issue with imbalanced classification datasets.

Choose metrics based on the cost of mistakes, not just the highest numerical score. In a spam filter, false positives can hide legitimate emails. In a screening system, false negatives may be more costly. The intended use determines which error deserves greater attention.

Binary vs. Multiclass Confusion Matrices

A binary confusion matrix evaluates two classes, such as positive and negative. A multiclass confusion matrix evaluates three or more classes, such as cat, dog, and bird. In a multiclass matrix, each row represents an actual class and each column represents a predicted class under the common actual-row convention. The diagonal cells represent correct classifications, and the off-diagonal cells represent errors.

A multiclass matrix can show which classes are commonly confused with one another. For example, a model may identify cats correctly but frequently classify dogs as wolves. To calculate binary metrics for a multiclass model, you can evaluate one class against all remaining classes using a one-vs-rest approach. In that calculation, the selected class is positive, and all other classes are negative. The official scikit-learn documentation explains how confusion matrix entries correspond to actual and predicted labels.

Common Confusion Matrix Calculation Mistakes

Avoid these errors when entering data or interpreting the output.

  • Mixing up false positives and false negatives. A false positive is an incorrect positive prediction; a false negative is a missed positive case.
  • Reversing actual and predicted labels. Check the matrix’s row and column headings before transferring the counts.
  • Relying on accuracy alone. A model can achieve high accuracy on an imbalanced dataset and still fail to detect the class you care about.
  • Comparing metrics from different datasets. Use consistent evaluation data when comparing models.
  • Ignoring zero denominators. Some metrics are undefined when their denominators equal zero. For example, precision is undefined when the model makes no positive predictions unless a particular reporting convention assigns a value.
  • Treating a score as proof of real-world performance. Results depend on the evaluation data, class distribution, prediction threshold, and intended application.

Checking these points helps you avoid incorrect conclusions from otherwise valid calculations.

References 

Scroll to Top