Confusion matrix calculator

The results of a classification model or a screening test can be summarized as counts of the four combinations of prediction and reality (the confusion matrix). This tool computes the evaluation metrics from them. Which metric matters most depends on whether misses (false negatives) or false alarms (false positives) are more costly.

Confusion matrix counts

Metrics

Accuracy
–
Precision
–
Recall (sensitivity)
–
F1 score
–
F-beta score
–
Specificity
–
False positive rate (FPR)
–
Negative predictive value (NPV)
–
Balanced accuracy
–
Matthews correlation coefficient (MCC)
–
Share of positives
–
Total count
–

Metrics whose denominator is 0 (such as precision when nothing is predicted positive) are shown as "–". MCC ranges from −1 to 1, where 1 is a perfect prediction and 0 is no better than random.

What is a confusion matrix?

A confusion matrix counts the results of binary classification in four cells by combining the actual class (positive or negative) with the prediction (positive or negative). A positive correctly predicted positive is a true positive (TP), a positive predicted negative is a miss, a false negative (FN), a negative predicted positive is a false alarm, a false positive (FP), and a negative correctly predicted negative is a true negative (TN). In the "Spam filter" example, 40 of 50 spam emails were caught (TP = 40, FN = 10) and 20 of 150 normal emails were wrongly flagged as spam (FP = 20, TN = 130).

Precision and recall

Precision = TP ÷ (TP + FP) is the share of predicted positives that are really positive, and reflects how few false alarms there are. Recall = TP ÷ (TP + FN) is the share of actual positives that were predicted positive, and reflects how few misses there are. In the example, precision is 40 ÷ 60 ≈ 66.67% and recall is 40 ÷ 50 = 80%. Lowering the decision threshold usually raises recall but adds false alarms and lowers precision, so the two trade off against each other.

F1 and F-beta

The F1 score is the harmonic mean of precision and recall, 2 × precision × recall ÷ (precision + recall). It is low whenever either one is low, so it summarizes the balance in one number. In the example it is 2 × 0.6667 × 0.8 ÷ 1.4667 ≈ 72.73%. When misses are worse, as in disease screening, the F-beta score = (1 + β²) × precision × recall ÷ (β² × precision + recall) weighs recall β times as much.

Imbalanced data and the accuracy trap

Accuracy = (TP + TN) ÷ total is easy to understand but useless when positives are rare. In the "Predict everything negative" example, all 10 positives are missed, yet accuracy is 990 ÷ 1000 = 99% while recall is 0%. In such cases evaluate with recall, precision and F1, with balanced accuracy (the average of the correct rates for positives and negatives), or with the Matthews correlation coefficient (MCC), which uses all four counts.

Practice related questions

A two-class classification model was evaluated on 200 test samples, and its confusion matrix shows 45 true positives (TP), 5 false positives (FP), 45 false negatives (FN) and 105 true negatives (TN). Which pair of precision and recall is correct?

  1. APrecision 0.50, recall 0.90
  2. BPrecision 0.75, recall 0.50
  3. CPrecision 0.90, recall 0.50
Show answer and explanation

Answer: C. Precision 0.90, recall 0.50

Precision is the fraction of items predicted positive that are actually positive: TP ÷ (TP + FP) = 45 ÷ 50 = 0.90. Recall is the fraction of actually positive items that were predicted positive: TP ÷ (TP + FN) = 45 ÷ 90 = 0.50. This model is almost always right when it says positive, but it misses half of the positives. Accuracy is (45 + 105) ÷ 200 = 0.75.

A two-class classification model was evaluated on 200 test samples, giving TP = 20, FP = 5, FN = 30 and TN = 145. Which is closest to the F1 score?

  1. A0.40
  2. B0.53
  3. C0.60
Show answer and explanation

Answer: B. 0.53

First, precision is 20 ÷ (20 + 5) = 0.8 and recall is 20 ÷ (20 + 30) = 0.4. The F1 score is the harmonic mean of precision and recall: 2 × 0.8 × 0.4 ÷ (0.8 + 0.4) = 0.64 ÷ 1.2 ≈ 0.53. The harmonic mean is pulled toward the smaller value, so the F1 score is not high when only one of the two is high. Note that the accuracy, (20 + 145) ÷ 200 = 0.825, looks high, yet this model misses 60% of the positives.

For data of 1000 items of which only 10 are positive, what is the accuracy of a model that predicts every item as negative?

  1. A0%
  2. B1%
  3. C99%
Show answer and explanation

Answer: C. 99%

Predicting everything as negative gets all 990 negatives right and all 10 positives wrong, so the accuracy is 990 ÷ 1000 = 99%. On the other hand, it finds no positives at all, so its recall is 0% and it is useless when the purpose is to find positives, as in detecting disease or fraud. For data in which the class proportions are heavily skewed (imbalanced data), models are therefore evaluated not by accuracy alone but together with precision, recall, the F1 score and so on. Countermeasures include weighting errors on the minority class more heavily and sampling to balance the number of data points per class.

FAQ

Should I favor precision or recall?

Decide by the cost of each kind of error. When a miss (false negative) is costly, as in disease screening or fraud detection, favor recall. When a false alarm (false positive) is costly, as when a spam filter hides an important email, favor precision.

How do recall, sensitivity, FPR and specificity relate?

Recall is called sensitivity (true positive rate) in medicine; it is the same metric. Specificity = TN ÷ (TN + FP) is the share of negatives correctly judged negative, and the false positive rate = 1 − specificity. A ROC curve plots the false positive rate on the horizontal axis against recall on the vertical axis as the threshold changes.

How is it computed for multi-class classification?

For each class, treat that class as positive and all others as negative, build a binary confusion matrix and compute precision, recall and F1. Averaging these per-class values is the macro average; summing TP, FP and FN over all classes before computing is the micro average.

How does this appear in exams and interviews?

Typical questions ask you to compute accuracy, precision, recall and F1 from confusion matrix counts, or to explain why accuracy is unreliable on imbalanced data. It is standard in machine learning courses and data science interviews.

Related terms

Confusion matrix Precision and recall Overfitting

Related calculators

Semiconductor, Electronics and Computing Calculators →