standarddeviationcalculator.net

Updated Free · runs in your browser

Statistics

Confusion matrix calculator

Four counts in, every classification metric out. Enter the true and false positives and negatives and the calculator lays out the matrix, each metric with its formula, and the working.

Actual positive, predicted positive.

Actual positive, predicted negative.

Actual negative, predicted positive.

Actual negative, predicted negative.

Accuracy 0.85
Precision0.8889
Recall (sensitivity)0.8
Specificity0.9
F1 score0.8421
MCC0.7035
Cohen's kappa0.7
Balanced accuracy0.85
Total (n)200
Predicted positivePredicted negativeTotal
Actual positiveTP = 80FN = 20100
Actual negativeFP = 10TN = 90100
Total90110200
MetricFormulaValue
Accuracy0.85—
Precision (PPV)0.8888888888888888—
Recall / sensitivity (TPR)0.8—
Specificity (TNR)0.9—
F1 score0.8421052631578947—
Negative predictive value0.8181818181818182—
False-positive rate0.1—
False-negative rate0.2—
False discovery rate0.1111111111111111—
Balanced accuracy0.8500000000000001—
Matthews correlation (MCC)0.7035264706814484—
Cohen's kappa0.7—
Youden's J0.7000000000000002—
Prevalence0.5—
0.85 Acc 0.889 Prec 0.8 Recall 0.9 Spec 0.842 F1 0.818 NPV 0.704 MCC 0.7 κ
Show the working, step by step
  1. Totals of the matrix:

    n = 80 + 20 + 10 + 90 = 200 actual positives = 100, actual negatives = 100, predicted positives = 90

  2. Accuracy is the share of all cases on the diagonal.

    (80 + 90) ÷ 200 = 0.85

  3. Precision divides by the predicted positives, recall by the actual positives.

    precision = 80 ÷ 90 = 0.888889 recall = 80 ÷ 100 = 0.8

  4. F1 is the harmonic mean of precision and recall.

    F1 = 2 × 80 ÷ (2 × 80 + 10 + 20) = 0.842105

  5. MCC uses all four cells, so it stays honest when the classes are unbalanced.

    MCC = (80×90 − 10×20) ÷ √(90×100×100×110) = 0.703526

  6. Kappa compares the observed agreement p₀ with the agreement pₑ expected by chance from the margins.

    pₑ = (100×90 + 100×110) ÷ 200² = 0.5 κ = (0.85 − 0.5) ÷ (1 − 0.5) = 0.7

The formulas

accuracy = (TP + TN) ÷ n precision = TP ÷ (TP + FP) recall = TP ÷ (TP + FN) specificity = TN ÷ (TN + FP) NPV = TN ÷ (TN + FN) F1 = 2TP ÷ (2TP + FP + FN) MCC = (TP·TN − FP·FN) ÷ √[(TP + FP)(TP + FN)(TN + FP)(TN + FN)] κ = (p₀ − pₑ) ÷ (1 − pₑ)

The false-positive rate is 1 − specificity and the false-negative rate is 1 − recall. Balanced accuracy is the mean of recall and specificity.

Worked example

The default matrix is a model tested on 200 cases, 100 of each class. It finds 80 of the 100 positives (TP = 80, FN = 20) and wrongly flags 10 of the 100 negatives (FP = 10, TN = 90).

  • Accuracy = (80 + 90) ÷ 200 = 0.85.
  • Precision = 80 ÷ 90 = 0.8889; recall = 80 ÷ 100 = 0.8; specificity = 90 ÷ 100 = 0.9.
  • F1 = 160 ÷ (160 + 10 + 20) = 0.8421, the harmonic mean of 0.8889 and 0.8.
  • NPV = 90 ÷ 110 = 0.8182; false-positive rate 0.1; false-negative rate 0.2.
  • MCC = (80 × 90 − 10 × 20) ÷ √(90 × 100 × 100 × 110) = 7,000 ÷ 9,949.9 = 0.7035.
  • Chance agreement pₑ = (100 × 90 + 100 × 110) ÷ 200² = 0.5, so κ = (0.85 − 0.5) ÷ 0.5 = 0.7.

Balanced accuracy is (0.8 + 0.9) ÷ 2 = 0.85, the same as accuracy because the classes are the same size.

How to interpret the metrics

Choose the metric that matches the cost of each mistake. When missing a positive is expensive (a cancer screen), watch recall. When false alarms are expensive (blocking a customer's card), watch precision or specificity. F1 balances precision and recall but ignores true negatives. MCC and kappa take every cell into account and stay honest when one class is rare; if they are near zero, the model is not doing much better than guessing from the class sizes.

Try changing the default to TP = 5, FN = 5, FP = 5, TN = 185. Accuracy is 0.95, which sounds good, but precision and recall are both 0.5 and MCC is about 0.47. The high accuracy comes mostly from the large negative class.

Common mistakes

  • Swapping FN and FP. A false negative is an actual positive the model missed; a false positive is an actual negative it flagged.
  • Reading the table the wrong way round. Some software puts predictions in rows and actual classes in columns. Check before you copy the numbers.
  • Averaging F1 across folds or classes without saying how. Macro and micro averages can differ a lot.
  • Reporting accuracy alone on unbalanced data. Add recall, precision or MCC.
Confusion matrix calculator: the worked example on this page, with its result and chart
Confusion matrix calculator: the worked example above, at a glance.

Common questions

What is a confusion matrix?

A 2×2 table that compares a classifier's predictions with the truth. The rows are the actual classes and the columns the predicted ones, giving four counts: true positives (TP), false negatives (FN), false positives (FP) and true negatives (TN). Every standard classification metric is a ratio of these four numbers.

What is the difference between precision and recall?

Precision = TP ÷ (TP + FP): of everything flagged positive, how much was right. Recall = TP ÷ (TP + FN): of everything actually positive, how much was found. A spam filter with high precision rarely junks real mail; one with high recall rarely lets spam through.

When should I use MCC instead of accuracy or F1?

When the classes are unbalanced. The Matthews correlation coefficient uses all four cells and is high only if the model does well on both classes. F1 ignores true negatives entirely, and accuracy can be high just by predicting the majority class. MCC runs from −1 (always wrong) through 0 (no better than chance) to +1 (perfect).

What does Cohen's kappa measure here?

Agreement between predictions and truth beyond what the row and column totals would give by chance. κ = (p₀ − pₑ) ÷ (1 − pₑ), where p₀ is the accuracy and pₑ the agreement expected by chance. Values above about 0.6 are usually read as substantial agreement.

Some metrics show a dash. Why?

A metric is undefined when its denominator is zero. If the model never predicts positive, for example, precision is 0 ÷ 0. The calculator shows a dash and says which count caused it, rather than reporting a misleading 0.