Skip to content

Confusion Matrix Calculator

Calculate accuracy, precision, recall, F1, specificity, MCC and Cohen’s kappa from a confusion matrix, with macro, micro and weighted averages for 3–4 classes.

Calculator Details

F1 score:0.872See results ↓

Two classes (positive and negative), or a 3- or 4-class matrix

Actual positive, predicted positive

Actual positive, predicted negative (type II error)

Actual negative, predicted positive (type I error)

Actual negative, predicted negative

Your Results

F1 score: 0.872

F1 score

0.872

Accuracy 97.5% · balanced 93.9% · 1,000 cases

Precision (PPV)

85.0%

Predicted positives that are right

Recall (sensitivity)

89.5%

Actual positives found

Specificity

98.3%

False positive rate 1.7%

NPV

98.9%

False negative rate 10.5%

Matthews correlation (MCC)

0.858

−1 to 1; uses all four cells

Cohen's kappa

0.858

Agreement beyond chance

Accuracy 97.5%, precision 85%, recall 89.5%, F1 0.872; MCC 0.858 and Cohen's kappa 0.858 over 1,000 cases.

Compare scenarios

Save these inputs as a scenario, change them, and compare the results side by side.

Loading, available in a moment

Precision, recall and F1 by class

Precision, recall and F1 by class

Detailed Breakdown

Confusion matrix (rows = actual, columns = predicted)
Actual \ predictedPositiveNegative
Positive85 ✓10
Negative15890 ✓

✓ marks correct predictions (the diagonal).

Metrics by class
ClassActual countPrecisionRecallF1
Positive9585.0%89.5%0.872
Negative90598.9%98.3%0.986

How This Calculator Works

How it's calculated

precision = TP ÷ (TP + FP), recall = TP ÷ (TP + FN), specificity = TN ÷ (TN + FP)

F1 = 2 × precision × recall ÷ (precision + recall)

MCC = (TP·TN − FP·FN) ÷ √((TP + FP)(TP + FN)(TN + FP)(TN + FN))

κ = (observed agreement − chance agreement) ÷ (1 − chance agreement)

Your numbers: precision = 85 ÷ (85 + 15) = 0.8500; recall = 85 ÷ (85 + 10) = 0.8947; F1 = 2 × 0.8500 × 0.8947 ÷ (0.8500 + 0.8947) = 0.8718. MCC = (85×890 − 15×10) ÷ √(…) = 0.8583.

Frequently Asked Questions

What is a confusion matrix?

A table that counts a classifier’s predictions against the true classes. For two classes it has four cells: true positives (TP), false positives (FP), false negatives (FN) and true negatives (TN). Every metric on this page, from accuracy to the Matthews correlation, is computed from those counts.

What is the difference between precision and recall?

Precision asks how many of the predicted positives are really positive: TP ÷ (TP + FP). Recall asks how many of the real positives were found: TP ÷ (TP + FN). In the default example, precision is 85% and recall 89.5%. Raising the decision threshold usually trades recall for precision.

Why is accuracy misleading on imbalanced data?

When one class is rare, predicting the common class almost always looks accurate. A model with TP 1, FP 1, FN 8 and TN 90 is 91% accurate yet finds only 11.1% of the positives. Balanced accuracy (55%), F1 and the Matthews correlation (0.205) reveal the problem.

What is the F1 score?

The harmonic mean of precision and recall, 2PR ÷ (P + R). It is high only when both are high, and ignores true negatives. The general Fβ weights recall β times as much as precision: F2 favours finding positives, F0.5 favours being right when you flag one.

Why use the Matthews correlation coefficient (MCC)?

MCC uses all four cells and stays near 0 for a classifier no better than chance, whatever the class balance. It runs from −1 (always wrong) through 0 to 1 (perfect). Chicco and Jurman (2020) argue it is more informative than accuracy and F1 for binary classification.

What does Cohen’s kappa measure?

Agreement between predictions and the truth beyond what chance would give, given how often each class is predicted and occurs. 0 means chance-level agreement and 1 perfect agreement. The default example has κ = 0.858.

Why does precision change with prevalence?

Sensitivity and specificity are properties of the classifier, but precision also depends on how common positives are. With the default’s rates, a population where only 1% are positive gives a precision of just 35.3%: most flagged cases are false alarms. Enter a prevalence in the advanced options to see it.

What are macro, micro and weighted averages?

For more than two classes, metrics are computed per class and then averaged. Macro gives every class equal weight; weighted weights each class by how many cases it has; micro pools every prediction and, for single-label data, equals accuracy. Macro is the strictest when small classes are predicted poorly.

Metrics from the counts you enter, with scikit-learn's definitions (undefined ratios reported as 0): accuracy, balanced accuracy, precision, recall, specificity, NPV, Fβ, Matthews correlation (binary and multi-class), Cohen's kappa, and PPV and NPV at another prevalence by Bayes' theorem. For evaluating models and tests, not for making clinical decisions. Not included: ROC and precision–recall curves and their AUC, thresholds, confidence intervals, multi-label metrics, cost-weighted metrics and matrices larger than 4×4.

Confusion Matrix Calculator: Precision, Recall, F1, MCC

By FindMySum · Last reviewed

A confusion matrix is the scorecard of a classifier. It counts, for every true class, how often the model predicted each class, so you can see not just how often it was right but what kind of mistakes it made. This calculator takes the four counts of a two-class matrix (or a 3×3 or 4×4 matrix) and works out the standard evaluation metrics: accuracy and balanced accuracy, precision, recall, specificity and negative predictive value, the F1 and Fβ scores, the Matthews correlation coefficient and Cohen's kappa. It is built for machine-learning models, quality checks and screening tests; it is not a tool for clinical decisions.

The four cells

For two classes, label one as positive (the thing you are trying to detect) and the other negative:

  • True positives (TP): positive, and predicted positive.
  • False positives (FP): negative, but predicted positive. A false alarm, or type I error.
  • False negatives (FN): positive, but predicted negative. A miss, or type II error.
  • True negatives (TN): negative, and predicted negative.

The metrics

  • Accuracy = (TP + TN) ÷ total: the share of predictions that are correct.
  • Precision (positive predictive value) = TP ÷ (TP + FP): when the model says positive, how often it is right.
  • Recall (sensitivity, true positive rate) = TP ÷ (TP + FN): how many of the real positives it finds.
  • Specificity (true negative rate) = TN ÷ (TN + FP), and NPV = TN ÷ (TN + FN).
  • Balanced accuracy = (recall + specificity) ÷ 2, which doesn't reward ignoring a rare class.
  • F1 = 2 × precision × recall ÷ (precision + recall), and Fβ, which counts recall β times as much as precision.
  • Matthews correlation coefficient (MCC) = (TP·TN − FP·FN) ÷ √((TP + FP)(TP + FN)(TN + FP)(TN + FN)), the correlation between predictions and truth.
  • Cohen's kappa = (observed agreement − agreement expected by chance) ÷ (1 − chance agreement).

All values match scikit-learn's implementations; when a ratio would be 0 ÷ 0, it is reported as 0, as scikit-learn does by default.

Worked example

The calculator opens on a model tested on 1,000 cases: 85 true positives, 15 false positives, 10 false negatives and 890 true negatives.

  • Accuracy: (85 + 890) ÷ 1,000 = 97.5%.
  • Precision: 85 ÷ 100 = 85.0%. Recall: 85 ÷ 95 = 89.5%. Specificity: 890 ÷ 905 = 98.3%.
  • F1: 2 × 0.85 × 0.895 ÷ (0.85 + 0.895) = 0.872.
  • MCC: (85 × 890 − 15 × 10) ÷ √(100 × 95 × 905 × 900) = 0.858. Cohen's kappa: 0.858.
  • Balanced accuracy: (0.895 + 0.983) ÷ 2 = 93.9%.

Only 9.5% of the cases are positive, which is why accuracy (97.5%) looks so much better than F1 (0.872): the large number of true negatives lifts accuracy without saying much about the positive class.

Prevalence changes precision

Recall and specificity describe the classifier itself, but precision also depends on how common positives are in the population you apply it to. With the example's recall and specificity, the calculator's prevalence option shows that if only 1% of cases were positive, precision would fall to about 35%: two of every three flags would be false alarms. This is Bayes' theorem at work, and the reason screening for rare conditions produces many false positives even with a good test.

More than two classes

For 3 or 4 classes, enter the matrix with actual classes as rows and predictions as columns. Each class gets its own precision, recall and F1, treating it as positive and the others as negative. They are then combined three ways: the macro average (every class counts equally), the weighted average (classes weighted by how many cases they have) and the micro average (pooling all cases, which equals accuracy). The multi-class MCC and Cohen's kappa summarise the whole matrix in one number. When a small class matters, watch the macro average: a model can score well on weighted and micro averages while failing the rare class entirely.

Choosing a metric

No single number fits every problem. Report the confusion matrix itself alongside the metrics, choose precision, recall or Fβ by the cost of each error, and prefer MCC or balanced accuracy to plain accuracy when classes are imbalanced.

More questions

Which is the positive class?

Whichever outcome you are trying to detect: spam, fraud, a defect, a disease marker. It doesn't have to be the 'good' outcome. Swapping which class you call positive swaps precision with NPV and recall with specificity, while accuracy, MCC and kappa stay the same.

Should I optimise for precision or recall?

It depends on the cost of each mistake. When a false alarm is expensive (blocking a legitimate payment), favour precision; when a miss is expensive (failing to flag fraud or a safety defect), favour recall. Choose the F-score's β to match: F2 if missing positives costs more, F0.5 if false alarms do.

What is a good F1 score?

There is no universal threshold: it depends on the problem and the class balance. Compare against a baseline, such as always predicting the majority class or a simple model, and against other models evaluated on the same test set. A rare positive class makes a high F1 much harder to reach than a balanced one.

How do I get the four counts from my model?

Run the model on a labelled test set that wasn't used in training, then count each combination of actual and predicted class. Libraries do this for you: scikit-learn's confusion_matrix, R's table() or caret::confusionMatrix, or a pivot table in a spreadsheet.

Can the Matthews correlation be negative?

Yes. A negative MCC means the predictions disagree with the truth more often than chance would: the classifier is systematically wrong, and inverting its predictions would do better. 0 means no better than random guessing; 1 means perfect.

Sources

This article explains how the calculator works and is general information, not financial, tax, legal or medical advice. Rules change; check the sources above or a qualified professional before you act.

Spotted a wrong result or a bug? Report a problem

May we count your visits with Invoker Analytics? It keeps a random visitor number in your browser. Your calculator inputs are never sent. Privacy policy