Skip to content

Accuracy, F1, balanced accuracy and MCC from a binary confusion matrix

score_binary_classification

Works out accuracy, F1, balanced accuracy and Matthews correlation coefficient from a 2 × 2 confusion matrix.

Runs
On the server
Level
Checked against a reference: exact arithmetic (Python fractions; one square root at 60 digits), checked against scikit-learn 1.9.1 and the primary source's six printed cases
Ask your assistant

What it is

Returns accuracy, F1, balanced accuracy and the Matthews correlation coefficient from a 2 × 2 confusion matrix. Use when a classifier's true and false positives and negatives have been counted; F1 is also the Dice overlap of two sets. Not for sensitivity, specificity or predictive values: use `stats_diagnostic_accuracy`; not for agreement between raters: use `stats_agreement_kappa`; not for scores at every threshold: use `stats_roc_curve`. A score that is 0/0 on the counts is withheld, never filled in; all-zero counts are refused; counts are whole numbers.

Field: Statistics and design.

What it does

You give

  • truePositive (required): True positives: actual positives the model predicted positive. Must be at least 0 and at most 9007199254740991.
  • falsePositive (required): False positives: actual negatives the model predicted positive. Must be at least 0 and at most 9007199254740991.
  • falseNegative (required): False negatives: actual positives the model predicted negative. Must be at least 0 and at most 9007199254740991.
  • trueNegative (required): True negatives: actual negatives the model predicted negative. Must be at least 0 and at most 9007199254740991.

No request is recorded for this tool.

Limits

  • Research arithmetic scoring predictions against outcomes you supply; it does not judge a model, choose a threshold or know whether the cases were held out, and is not for diagnosis or treatment decisions.
  • Scores from the four counts you supply, the positive class as you labelled it.
  • Accuracy, F1 and the Matthews coefficient change with the class balance and do not carry over to data balanced otherwise.
  • A zero-over-zero score is withheld, never filled in.
  • Every computed number a reply returns, other than an integer or an exact decimal, is rounded once to 12 significant figures: to the nearest, a change of at most 5 × 10⁻¹² of its value, except that a lower bound is rounded down and an upper bound up, a change of less than 1 × 10⁻¹¹; the method computes, and decides any pass or fail, on the unrounded value.

It will not answer when

  • all four counts are zero: there is nothing to score.
  • a count is above the method's range, beyond which the sums of the counts are no longer held exactly.
Every input, with its units4 inputs
  • truePositive (required): True positives: actual positives the model predicted positive. Must be at least 0 and at most 9007199254740991.
  • falsePositive (required): False positives: actual negatives the model predicted positive. Must be at least 0 and at most 9007199254740991.
  • falseNegative (required): False negatives: actual positives the model predicted negative. Must be at least 0 and at most 9007199254740991.
  • trueNegative (required): True negatives: actual negatives the model predicted negative. Must be at least 0 and at most 9007199254740991.
Every refusal, with its code2 refusals
CodeWhen
NO_OBSERVATIONSall four counts are zero: there is nothing to score.
COUNTS_ABOVE_RANGEa count is above the method's range, beyond which the sums of the counts are no longer held exactly.

The calculation

The method's equations, constants and worked example.

Accuracy, F1, balanced accuracy and Matthews correlation coefficient of a binary classificationscore-binary-classification@1.0.1

Equation 1

N=TP+FP+FN+TN

accuracy = (TP + TN)/N

SymbolMeaning
TPtrue positives: actual positives predicted positive (argument truePositive)
FPfalse positives: actual negatives predicted positive (argument falsePositive)
FNfalse negatives: actual positives predicted negative (argument falseNegative)
TNtrue negatives: actual negatives predicted negative (argument trueNegative)
Nthe number of observations scored
accuracythe fraction of observations predicted correctly, returned as accuracy

Source 1.

Equation 2

F1=2·TP/(2·TP+FP+FN)
SymbolMeaning
TPtrue positives: actual positives predicted positive (argument truePositive)
FPfalse positives: actual negatives predicted positive (argument falsePositive)
FNfalse negatives: actual positives predicted negative (argument falseNegative)
F1the harmonic mean of precision and recall in its count form, returned as f1Score; undefined when TP + FP + FN = 0

Source 1.

Equation 3

TPR=TP/(TP+FN)
TNR=TN/(TN+FP)

balanced accuracy = (TPR + TNR)/2

SymbolMeaning
TPtrue positives: actual positives predicted positive (argument truePositive)
FPfalse positives: actual negatives predicted positive (argument falsePositive)
FNfalse negatives: actual positives predicted negative (argument falseNegative)
TNtrue negatives: actual negatives predicted negative (argument trueNegative)
TPRtrue positive rate (sensitivity, recall); undefined when there is no actual positive
TNRtrue negative rate (specificity); undefined when there is no actual negative
balanced accuracythe mean of the two rates, returned as balancedAccuracy

Source 2.

Equation 4

MCC=(TP·TN−FP·FN)/√((TP+FP)·(TP+FN)·(TN+FP)·(TN+FN))
SymbolMeaning
TPtrue positives: actual positives predicted positive (argument truePositive)
FPfalse positives: actual negatives predicted positive (argument falsePositive)
FNfalse negatives: actual positives predicted negative (argument falseNegative)
TNtrue negatives: actual negatives predicted negative (argument trueNegative)
MCCMatthews correlation coefficient, from −1 to 1, returned as matthewsCorrelation; undefined when any of the four sums under the root is zero

Source 1.

A definition, not a model: the equation states what the quantity means. A different convention gives a different number.

Worked example

Worked example: one case of score-binary-classification@1.0.1, from its inputs through each step to the answer. Values are written as a person writes them; the working carries more digits, so your own figure may differ in the last digit shown.

Inputs

  • truePositive = 6
  • falsePositive = 2
  • falseNegative = 3
  • trueNegative = 9

Steps

  1. total = 20, from truePositive, falsePositive, falseNegative, trueNegative; by equation 1.
  2. accuracy = 0.75, from truePositive, trueNegative, total; by equation 1; gives accuracy.
  3. f1 = 0.70588235, from truePositive, falsePositive, falseNegative; by equation 2; gives f1Score.
  4. truePositiveRate = 0.66666667, from truePositive, falseNegative; by equation 3.
  5. trueNegativeRate = 0.81818182, from trueNegative, falsePositive; by equation 3.
  6. balancedAccuracy = 0.74242424, from truePositiveRate, trueNegativeRate; by equation 3; gives balancedAccuracy.
  7. numerator = 48, from truePositive, falsePositive, falseNegative, trueNegative; by equation 4.
  8. marginalProduct = 9504, from truePositive, falsePositive, falseNegative, trueNegative; by equation 4.
  9. root = 97.488461, from marginalProduct; by equation 4.
  10. mcc = 0.49236596, from numerator, root; by equation 4; gives matthewsCorrelation.

The working, as the case writes it:

N = 20

accuracy = 15/20 = 0.75

F1 = 12/(12 + 2 + 3) = 12/17 = 0.70588235

TPR = 6/9 = 0.66666667, TNR = 9/11 = 0.81818182, balanced accuracy = 0.74242424

MCC = (6·9 − 2·3)/√(8·9·11·12) = 48/√9504 = 48/97.488461 = 0.49236596 (worked by hand by Drybench on 12 November 2026

not an independent check)

Answer

accuracy = 0.75

f1Score = 0.70588235

balancedAccuracy = 0.74242424

matthewsCorrelation = 0.49236596

Where these values came from: our own exact-arithmetic script and a hand-worked case; stored reference values (fractions; 60-digit decimal square root).

At build the tool returned this answer within the tolerance the file states: accuracy, F1, balanced accuracy and MCC relative 1e-12, with a floor of 1e-18 below 1e-6; statuses, codes, withholding rules and units exact.

Sources

  1. Chicco D, Jurman G, The advantages of the Matthews correlation coefficient (MCC) over F1 score and accuracy in binary classification evaluation, BMC Genomics 21:6 (2020)

    Used for: Accuracy, F1, balanced accuracy and Matthews correlation coefficient of a binary classification. At: equation 1, section Notation and mathematical foundations; equation 4, section Notation and mathematical foundations; equation 2, section Notation and mathematical foundations.

    Read in full, 12 November 2026
  2. Chicco D, Tötsch N, Jurman G, The Matthews correlation coefficient (MCC) is more reliable than balanced accuracy, bookmaker informedness, and markedness in two-class confusion matrix evaluation, BioData Mining 14:13 (2021)

    Used for: Accuracy, F1, balanced accuracy and Matthews correlation coefficient of a binary classification. At: equation 6, section Introduction.

    Read in full, 12 November 2026

How it is checked

Level

Checked against a reference.

Checked against

Our own exact-arithmetic script and a hand-worked case; the source's own worked example, Chicco and Jurman 2020, Table 4 (printed values), version of record.

Cases

No case set is linked to this version.

Not checked

The predictions, the decision threshold and the test set behind the counts; scores for more than two classes; the fill-in values the first source proposes for undefined scores, which this method does not adopt; Matthews (1975), the coefficient's primary source, unread; any sampling interval.

Changes to its methods0 changes

No change is logged for these method versions.