Skip to content
Raisalon

Machine learning tool

Classification Metrics Calculator

Calculate accuracy, precision, recall, F1, specificity, balanced accuracy, and Matthews correlation coefficient from a binary confusion matrix.

Confusion matrix

Enter non-negative whole numbers.

Actual positive, predicted positive

Actual positive, predicted negative

Actual negative, predicted positive

Actual negative, predicted negative

Accuracy
0.9300
(TP + TN) / total
Precision
0.8400
TP / (TP + FP)
Recall
0.8750
TP / (TP + FN)
F1 score
0.8571
2TP / (2TP + FP + FN)
Specificity
0.9474
TN / (TN + FP)
Balanced accuracy
0.9112
(recall + specificity) / 2
MCC
0.8111
Matthews correlation, −1 to 1
Prevalence
0.2400
(TP + FN) / total
Total samples
200

“undefined” means the metric’s denominator is zero for these counts (for example, precision when there are no positive predictions).

How to use it

Enter the four counts from a binary classifier’s confusion matrix. All metrics update immediately. A metric shows as “undefined” when its denominator is zero; for example, precision is undefined when the model made no positive predictions.

The confusion matrix

A binary classifier’s predictions fall into four groups:

Actual \ predicted Predicted positive Predicted negative
Actual positive True positive (TP) False negative (FN)
Actual negative False positive (FP) True negative (TN)

Every metric in this calculator is a different summary of these four numbers.

Which metric to use

Accuracy is the share of all predictions that were correct. It is easy to explain but misleading on imbalanced data: if 1% of transactions are fraudulent, a model that never predicts fraud is 99% accurate and completely useless.

Precision answers “when the model says positive, how often is it right?” Optimise for precision when false positives are expensive, such as flagging legitimate emails as spam.

Recall (sensitivity, true positive rate) answers “of all actual positives, how many did the model find?” Optimise for recall when missing a positive is costly, such as screening for a disease.

F1 score is the harmonic mean of precision and recall:

F1=2⋅precision⋅recallprecision+recall=2 TP2 TP+FP+FNF_1 = \frac{2 \cdot \text{precision} \cdot \text{recall}}{\text{precision} + \text{recall}} = \frac{2\,TP}{2\,TP + FP + FN}

The harmonic mean is pulled toward the smaller value, so F1 is only high when both precision and recall are high. Note that F1 ignores true negatives entirely.

Specificity (true negative rate) is recall for the negative class: the share of actual negatives correctly identified.

Balanced accuracy averages recall and specificity, giving each class equal weight regardless of how many samples it has.

Matthews correlation coefficient (MCC) uses all four cells:

MCC=TP⋅TN−FP⋅FN(TP+FP)(TP+FN)(TN+FP)(TN+FN)\text{MCC} = \frac{TP \cdot TN - FP \cdot FN}{\sqrt{(TP + FP)(TP + FN)(TN + FP)(TN + FN)}}

It ranges from −1 (complete disagreement) through 0 (no better than chance) to 1 (perfect prediction). Because it accounts for every type of error, MCC is a strong single-number summary for imbalanced problems.

Checking your own code

The same metrics are available in scikit-learn, which is a useful cross-check:

from sklearn.metrics import f1_score, matthews_corrcoef, precision_score, recall_score

y_true = [1, 1, 0, 0, 1, 0]
y_pred = [1, 0, 0, 1, 1, 0]

print(precision_score(y_true, y_pred))   # 0.667
print(recall_score(y_true, y_pred))      # 0.667
print(f1_score(y_true, y_pred))          # 0.667
print(matthews_corrcoef(y_true, y_pred)) # 0.333

More tools

  • Developer

    JSON Formatter and Validator

    Format, minify, validate, and sort JSON in your browser, with error messages that point to the exact line and column of a syntax problem.

    Open tool

  • Developer

    Base64 Encoder and Decoder

    Encode text to Base64 or decode Base64 back to text, with full UTF-8 support and an optional URL-safe alphabet for tokens and URLs.

    Open tool

  • Developer

    Unix Timestamp Converter

    Convert Unix timestamps in seconds or milliseconds to readable UTC and local dates, and convert dates back to timestamps, with live current time.

    Open tool