ForHosting KIT · Math

Confusion matrix calculator

Eight cases compared with their predictions give 3 true positives, 3 true negatives, 1 false positive and 1 false negative. Paste the true and the predicted labels in the same order to get the four counts behind accuracy, precision and recall. Free in your browser.

  • Free
  • Runs in your browser
  • No sign-up
  • Your data stays on your device
Use it from WebAPIEmailTelegramApp soon

A confusion matrix calculator converts two aligned sequences — the true labels you already know and the predicted labels a model, test, or rule produced — into the four binary counts that every classification report begins with: true positives, true negatives, false positives, and false negatives. You send the paired arrays and, when the problem is not an obvious two-class pair such as one versus zero, you name the positive class. The engine walks each index once and increments the matching cell. There is no network, no randomness, and no clock.

What a confusion matrix counts and why the four cells matter

A binary confusion matrix is the four-cell table that records how a classifier’s predictions line up with the ground-truth labels. Each paired observation falls into exactly one cell. A true positive is a case whose true label is the positive class and whose predicted label is also the positive class: the model correctly raised the alarm.

A true negative is a case whose true label is not the positive class and whose prediction is also not the positive class: the model correctly stayed quiet. A false positive is a type I error, a false alarm: the true label is negative but the model predicted positive. A false negative is a type II error, a miss: the true label is positive but the model predicted negative.

Those four integers — true positives, true negatives, false positives, and false negatives — are the raw material for accuracy, precision, recall, specificity, F1, and Matthews correlation, but this capability stops at the counts themselves so you can compose those derived scores however your report requires. The walk is one-versus-rest when you name a positive class among more than two labels: every other label is treated as negative.

The implementation is a single deterministic pass with no sampling, no network, and no dependence on the current date, which makes the four counts safe to pin in goldens and homework keys. Use it when you already have aligned true and predicted labels and need the four cells, not a full multi-class table of every pair of classes.

Who relies on the four confusion matrix counts

Medical and screening tutorials use the same four counts to explain sensitivity and specificity: true positives are the sick patients the test caught, false negatives are the misses, false positives are the healthy people sent for follow-up, and true negatives are the correctly cleared cases. Spam, fraud, and moderation queues watch false positives because each one wastes a human review slot, and they watch false negatives because each one is a message that leaked. Failed validation does not consume balance.

Keep the two lists aligned, name the positive class when it is not obvious, and you get reproducible TP, TN, FP, and FN for homework, CI, and production reports without standing up a statistics stack of your own.

Screening and diagnostic teaching

Show how a medical or security test splits patients into hits, misses, false alarms, and correct all-clears from two label lists.

Homework and auto-grading

Pin a known 0/1 example so a grader can mark the four cells without accepting a swapped off-diagonal as correct.

How do I build a confusion matrix from true and predicted labels?

Optionally set positive to name the positive class.

What happens if the two label arrays differ in length?

Pairing leftover items would invent matches, so unequal lists are rejected and are not billed.

How is the positive class chosen if I omit it?

Known pairs such as 1/0, true/false, yes/no, and spam/ham are inferred. Any other two-class problem uses the lexicographically last distinct label. More than two classes requires an explicit positive field.

Can I score a multi-class problem?

Yes, as one-versus-rest: set positive to the class you care about. Every other label counts as negative. The capability returns the four binary cells, not a full k-by-k table.

What is the difference between a false positive and a false negative?

A false positive is a false alarm, a type I error: the true label is negative but the prediction says positive. A false negative is a miss, a type II error: the case was positive but the prediction said negative.

Everything on this page is available programmatically. This section is for teams who want to wire it into their own systems; everyone else can just use the tool above.

POSThttps://api.kit.forhosting.com/stat/confusion-matrix

Prefer to automate it? One authenticated POST creates the task; the result comes back by webhook or a signed link. The same capability also runs here on the web, by email and from Telegram — and soon from our app too.

curl -X POST https://api.kit.forhosting.com/stat/confusion-matrix \
  -H "Authorization: Bearer $KIT_KEY" \
  -H "Content-Type: application/json" \
  -d '{"true_labels":["1","1","0","1","0","0","1","0"],"predicted_labels":["1","0","0","1","0","1","1","0"]}'
{
  "true_labels": [
    "1",
    "1",
    "0",
    "1",
    "0",
    "0",
    "1",
    "0"
  ],
  "predicted_labels": [
    "1",
    "0",
    "0",
    "1",
    "0",
    "1",
    "1",
    "0"
  ]
}
{
  "task_id": "tsk_a1b2c3d4e5f6a1b2c3d4e5f6",
  "type": "stat.confusion_matrix",
  "status": "queued",
  "_links": {
    "result": "/tasks/tsk_…/result"
  }
}

The API is asynchronous: the call returns a task_id immediately and the result arrives by webhook. Polling is capped at 1 req/s per task.

Per request$0.002

Published price — no tokens, no invented credits. A failed task is never charged.

max_n10000
HTTPCodeMeaning
401unauthorizedMissing or invalid API key.
402insufficient_balanceYour balance doesn't cover the task price.
404unknown_typeThat task type doesn't exist.
429rate_limitedToo many requests. Use the webhook instead of polling.

If the two arrays differ in length the task fails with invalid_input instead of inventing pairs or dropping leftovers. Successful API calls cost $0.002; bad input is not billed.

What a confusion matrix counts and why the four cells matter

When the two input arrays do not have the same length, pairing would be invented, so the request is rejected with invalid_input rather than truncated or padded.

Input contract, length check, and how the positive class is chosen

Numbers and booleans are coerced to strings so that the number one and the token 1 count as the same label. The optional positive field names the class treated as the positive cell of the two-by-two table. When you omit it, known complementary pairs such as 1 and 0, true and false, yes and no, positive and negative, pos and neg, p and n, or spam and ham are inferred; if only the negative token of a known pair appears, the complementary positive name is used so an all-zero list becomes all true negatives rather than a misleading all-true-positive table. For any other two-class problem the lexicographically last distinct label is taken as positive, matching the usual sorted-label convention. More than two distinct labels without an explicit positive is an error, because a four-cell matrix would otherwise pick a class in silence. On success the body returns n, the resolved positive label, an optional negative label when exactly one complement is present, the four counts, and a short formula string. POST a JSON body with two required fields: true_labels, the ground-truth class of each observation, and predicted_labels, the class the model assigned to that same observation. Aliases y_true, actual, actual_labels, and labels_true are accepted for the first list; y_pred, predicted, preds, and labels_pred are accepted for the second, so notebooks and typed forms can reuse the names they already have. Each field may be a JSON array of strings, finite numbers, or booleans, or a single comma-separated or whitespace-separated string when a form posts plain text. Empty tokens, objects, Infinity, and NaN fail with invalid_input naming the field. Both lists must contain at least one label and at most ten thousand; longer arrays are rejected so memory stays bounded on the free browser path and on the Worker. The decisive rule is equal length: if true_labels and predicted_labels differ in length the task fails immediately, because there is no honest way to pair leftover items. Nothing is stored after the response returns.

Where a confusion-matrix API earns its keep

Machine-learning evaluation scripts need a tiny, deterministic oracle that turns a hold-out list of y_true and y_pred into the four cells before they compute precision or recall, without pulling a full metrics library into a constrained Worker or a browser demo. Product dashboards that already store predictions beside labels can call the API from a pipeline and store only the four integers, which are cheap to trend over time.

Model evaluation scripts

Turn aligned y_true and y_pred from a hold-out set into TP, TN, FP, and FN before you compute precision, recall, or F1 in CI.

What does the confusion matrix calculator cost?

The browser tool is free. API requests use prepaid balance at $0.002 per successful call; invalid input is not billed.

Read the full KIT documentation →