Visual explainer
Confusion Matrix
Read forty hits, ten false alarms, twenty misses and thirty correct negatives, then see what the table reveals and what it loses.
A confusion matrix counts predictions against known labels, separating correct decisions from different kinds of mistakes. Read the axis labels before the cells: this page consistently uses predicted rows and actual columns, with positive first. A single matrix describes one dataset and one decision rule; it cannot recover the original scores.
Why can accuracy hide missed positives?
The opening fixture has 90 actual negatives and 10 actual positives. A model predicting negative for everyone gets 90 of 100 right, so accuracy is 0.90. Yet all 10 positives are missed: recall is . The strip's lengths encode the two case counts, and the plus inside the short segment identifies actual positives despite their negative predictions. Precision is undefined here because there are no positive predictions; a library may report a configured replacement value.
Where does each prediction go?
A predicted positive that is actually negative belongs in the highlighted false-positive cell. TP means true positive, FP false positive, FN false negative and TN true negative. The diagonal contains correct decisions. A false alarm is FP; a missed positive is FN. Positive names the class of interest, not a favorable outcome.
This convention is explicit rather than universal. scikit-learn's confusion_matrix API uses actual rows and predicted columns; its usual negative-first label order also differs from this positive-first drawing. Check both axis orientation and label order before comparing cells or flattening an array.
Do the counts conserve every case?
From this point, change to a new synthetic 100-case fixture: TP40, FP10, FN20 and TN30. It contains 60 actual positives and 40 actual negatives, unlike the opening rare-class example. Matrix cells have equal drawing area because this is a table; their printed numbers carry the counts.
The predicted-positive row totals ; predicted-negative totals . The actual-positive column totals ; actual-negative totals . Both margins reconcile to 100. Correct decisions total , so accuracy is 0.70. The small bars beside and below the table identify the margins, not additional cases.
What fraction of positive predictions is genuine?
Select the predicted-positive row: 40 TP and 10 FP. Here strip length encodes count, so the mint segment occupies four fifths of all positive predictions:
The denominator is determined by the model's positive calls. Precision answers how many such calls were genuine, not how many actual positives were found. With zero positive calls the fraction has a zero denominator; report that condition and the chosen handling policy.
How does F1 combine the two error types?
The picture uses the count form of F1. Its long segment has length proportional to twice the true positives: . The remaining lengths represent 10 false positives and 20 false negatives. The total is 110 weighted count units, not 110 distinct examples:
This equals the harmonic mean of precision 0.80 and recall . F1 ignores TN and gives FP and FN equal coefficients in this count formula; it is not a substitute for a real cost model. If TP, FP and FN are all zero, the formula is undefined. The metrics guide describes these formulas and zero-division choices.
What happens when the cutoff moves?
P abbreviates predicted and A actual; the axes retain the same orientation. In this stipulated example, lowering the threshold moves 10 actual positives from FN to TP and 10 actual negatives from TN to FP. The right matrix therefore contains TP50, FP20, FN10 and TN20. Actual class totals remain 60 and 40, while positive predictions increase from 50 to 70.
One concrete score allocation that produces the change is: the original 40 TP score 0.8, 10 FP score 0.7, 10 of the FN and 10 of the TN score 0.4, and the remaining 10 FN and 20 TN score 0.1. Predict positive when score is at least the cutoff. Moving from 0.5 to 0.3 produces exactly the pictured counts. These are synthetic scores, not calibrated probabilities.
Recall rises from to ; precision falls from to . Accuracy stays . On fixed scores, lowering the cutoff cannot reduce the number of detected positives, but precision need not move monotonically. Choose a threshold on validation data using the intended objective; do not tune it against the final test. See the threshold guide.
Which class was confused with which?
This separate multiclass example has 144 cases, not the binary fixture's 100. Rows are predicted cat/dog/bird; columns are actual cat/dog/bird. The highlighted 7 means seven actual dogs were predicted as cats. The reverse direction is the 3 in the next row's first column.
The diagonal sums to correct cases. Row totals are 59,54,31; column totals are 54,54,36; both sum to 144. Accuracy is . A three-class matrix has six ordered off-diagonal mistake types. These cells identify what to investigate; they do not establish whether the cause is insufficient data, label ambiguity or unsuitable features.
What fraction of actual positives was detected?
Return to the baseline binary fixture, before changing its cutoff. Select the actual-positive column: 40 TP and 20 FN. The vertical lengths encode those case counts, so the detected segment occupies two thirds of the whole column:
The same 40 hits appear in the precision numerator, but recall's denominator is determined by the known actual labels. A normalized actual-positive column contains detected and missed. With no actual positives, recall is undefined; a replacement value does not provide evidence about detection. This vertical picture uses its own count scale; compare fractions, not pixel lengths, with the earlier horizontal precision strip.
What if the two errors have different costs?
These bars use a new quantity: cost units, not case counts. Assume each FP costs 1 unit, each FN costs 5, and correct decisions cost zero. The baseline cost is . The lower-cutoff matrix from scene 6 costs . The short segment in each bar represents FP cost; the long segment represents FN cost. Both bars share the same scale.
Both matrices have 0.70 accuracy, yet their stipulated costs differ. If FP and FN instead cost the same 1 unit, each has total cost 30. The preferred cutoff therefore depends on the actual consequences and operating population; these example weights are not a recommendation for any application.
What information does a matrix lose?
The two rulers show new synthetic score allocations on the same four baseline case groups. Each circle represents actual positives and each diamond actual negatives; a label such as40+ means a group of 40 actual-positive cases, not a score of 40. Horizontal position encodes score on the common 0 to 1 scale. The dashed line is cutoff 0.5, and every case at or above it is predicted positive.
For A, the TP40, FP10, FN20 and TN30 groups have scores 0.9,0.6,0.4 and0.1. For B, those same groups have scores 0.6,0.9,0.1 and0.4. At cutoff 0.5, both produce the identical 40/10/20/30 matrix. Yet B ranks its false-positive group above its detected positives; A ranks these groups in the opposite order.
Lower the cutoff to0.3: A yields TP60, FP10, FN0, TN30; B yields TP40, FP40, FN20, TN0. The same original matrix cannot predict which change will happen because it discarded the score ordering. Keep scores when evaluating rankings or sweeping cutoffs; use a calibration check when probabilities matter. A matrix also cannot by itself establish performance on future data or unrecorded subgroups. Its useful compression is also its honest limitation.
The Quick Version
- Check axis orientation and label order before naming a cell.
- Precision conditions on predicted positives; recall conditions on actual positives.
- F1 uses twice TP against FP and FN and ignores TN.
- Threshold changes preserve actual class totals while moving predictions.
- Equal accuracy need not imply equal error cost.
- Equal matrices at one cutoff can hide different score rankings.