← CS Lab AI & Machine Learning / Confusion Matrix Open standalone ↗

A confusion matrix is the scorecard behind every classifier metric. It splits predictions into four buckets, and precision, recall, F1 and accuracy are all just ratios of those four numbers.

Where you meet it

What it does

It cross-tabulates actual labels against predicted labels. The diagonal is what the model got right; everything off the diagonal is an error. One table separates the two ways of being correct from the two ways of being wrong.

One number can't tell you which way you're wrong — but the four cells always can. That's why accuracy hides the mistake that costs you.

How it works

For a binary problem you compare actual (rows) against predicted (columns), giving four counts:

             pred +      pred −
actual +      TP          FN
actual −      FP          TN

TP — predicted +, really +   (correct hit)
FP — predicted +, really −   (false alarm)
FN — predicted −, really +   (missed positive)
TN — predicted −, really −   (correct reject)

The headline metrics are built straight from these counts:

Precision = TP / (TP + FP)   # of the things you flagged, how many were right
Recall    = TP / (TP + FN)   # of the real positives, how many you caught
F1        = 2 · P · R / (P + R)   # harmonic mean of precision & recall
Accuracy  = (TP + TN) / (TP + FP + FN + TN)

Precision punishes false alarms; recall punishes misses. F1 is one number for when both matter — the harmonic mean stays low unless both are high. Accuracy is the overall fraction correct.

Watch out

  • Accuracy lies on imbalanced data. If 99% of cases are negative, always predicting "no" scores 99% while catching zero positives. Look at precision/recall per class instead.
  • Precision and recall trade off. Raising the threshold cuts false alarms (FP↓) but adds misses (FN↑). You move along a curve — you rarely get both for free.
  • Decide which error is costlier. A missed tumor (FN) is not the same as a false alarm (FP). Optimize for the expensive one, not for a single tidy number.
  • Check the axis convention. scikit-learn uses rows = true, columns = predicted, and the binary order is TN, FP, FN, TP — many textbooks flip it. Read the labels before trusting a cell.

Go deeper

Die Konfusionsmatrix ist die Punktetabelle hinter jeder Classifier-Kennzahl. Sie teilt Vorhersagen in vier Felder — und Precision, Recall, F1 und Accuracy sind allesamt nur Verhältnisse dieser vier Zahlen.

Wo es vorkommt

Was es tut

Sie kreuztabelliert echte Labels gegen vorhergesagte Labels. Die Diagonale ist, was das Modell richtig hatte; alles abseits der Diagonale ist ein Fehler. Eine Tabelle trennt die zwei Arten, richtig zu liegen, von den zwei Arten, falsch zu liegen.

Eine Zahl verrät nicht, wie du danebenliegst — die vier Felder schon. Darum verbirgt Accuracy genau den Fehler, der dich kostet.

Wie es funktioniert

Bei einem binären Problem stellst du echt (Zeilen) gegen vorhergesagt (Spalten) — das ergibt vier Zählwerte:

             pred +      pred −
echt +        TP          FN
echt −        FP          TN

TP — vorhergesagt +, wirklich +   (Treffer)
FP — vorhergesagt +, wirklich −   (Fehlalarm)
FN — vorhergesagt −, wirklich +   (verpasstes Positiv)
TN — vorhergesagt −, wirklich −   (korrekte Ablehnung)

Die zentralen Kennzahlen entstehen direkt aus diesen Zählwerten:

Precision = TP / (TP + FP)   # von dem, was du markiert hast, wie viel war richtig
Recall    = TP / (TP + FN)   # von den echten Positiven, wie viele hast du erwischt
F1        = 2 · P · R / (P + R)   # harmonisches Mittel aus Precision & Recall
Accuracy  = (TP + TN) / (TP + FP + FN + TN)

Precision bestraft Fehlalarme; Recall bestraft Verpasste. F1 ist die eine Zahl, wenn beides zählt — das harmonische Mittel bleibt niedrig, solange nicht beide hoch sind. Accuracy ist der Gesamtanteil korrekter Vorhersagen.

Worauf achten

  • Accuracy lügt bei Klassenunbalance. Sind 99% der Fälle negativ, gibt „immer nein" 99% — und fängt null Positive. Schau stattdessen auf Precision/Recall pro Klasse.
  • Precision und Recall tauschen gegeneinander. Die Schwelle anheben senkt Fehlalarme (FP↓), erhöht aber Verpasste (FN↑). Du bewegst dich auf einer Kurve — beides gratis gibt es selten.
  • Entscheide, welcher Fehler teurer ist. Ein übersehener Tumor (FN) ist nicht dasselbe wie ein Fehlalarm (FP). Optimiere auf den teuren Fehler, nicht auf eine hübsche Einzelzahl.
  • Achsenkonvention prüfen. scikit-learn nutzt Zeilen = echt, Spalten = vorhergesagt, und die binäre Reihenfolge ist TN, FP, FN, TP — viele Lehrbücher drehen das um. Lies die Beschriftung, bevor du einer Zelle traust.

Mehr dazu

Next up
AI & Machine LearningROC & AUC AI & Machine LearningBias–Variance & Overfitting All concepts →