Confusion matrices
What Are Confusion Matrices?
Confusion matrices are tabular summaries of a classifier's performance in which rows correspond to actual classes and columns to predicted classes, so that each cell counts how many instances of one class were assigned to another. They are the primary diagnostic instrument in supervised classification because they preserve the full pattern of errors rather than collapsing it into a single number. The name reflects what the table exposes: which classes a model confuses with which others, and in what direction.
For a two-class problem the matrix has four cells, conventionally labeled true positives, false positives, false negatives, and true negatives. Almost every scalar metric used to evaluate a binary classifier is a function of those four counts, which is why the matrix is treated as the underlying representation and the metrics as derived views. The same construction is used in remote sensing, where the equivalent table is called an error matrix, and in medical diagnostics, where it underlies sensitivity and specificity.
Structure and Derived Metrics
Accuracy, the proportion of correct predictions, is the simplest quantity read off the diagonal, but it is misleading whenever class frequencies are unequal: a classifier that predicts the majority class for every instance in a dataset with one percent positives still scores 99 percent. Precision measures how many predicted positives are genuinely positive, while recall, also called sensitivity or the true positive rate, measures how many actual positives were found. The F-measure combines the two as a harmonic mean. Specificity, the true negative rate, complements recall on the other class. A treatment of evaluation from precision and recall through informedness and markedness by David Powers argues that precision, recall, F-measure, and raw accuracy are all biased by class prevalence, and proposes chance-corrected alternatives, including informedness and the Matthews correlation coefficient, that remain interpretable when the classes are unbalanced.
Threshold Selection and ROC Analysis
Most classifiers produce a continuous score, and the confusion matrix is defined only after a decision threshold has been applied. Varying that threshold traces a curve through the space of achievable true positive and false positive rates, which is the receiver operating characteristic. Tom Fawcett's introduction to ROC analysis in Pattern Recognition Letters explains why this view is useful when class distributions are skewed or when the cost of a false positive differs from that of a false negative, since ROC geometry is invariant to changes in class prior while precision is not. Precision-recall curves are preferred when the positive class is rare and the negative class is of little interest. In either case, the operating point chosen from the curve determines the single confusion matrix that will be reported.
Multiclass and Imbalanced Settings
With more than two classes, the matrix becomes square with one row and column per class, and the off-diagonal structure becomes the informative part. A block of mutual confusion between two classes indicates that they are not separable under the current feature representation, while an asymmetric pattern suggests a prior or sampling problem rather than a representational one. Per-class metrics are computed by treating each class in turn as positive, then aggregated by micro-averaging, which weights by instance count, or macro-averaging, which weights each class equally and is the appropriate choice when rare classes matter. Standard implementations such as the confusion matrix function in scikit-learn support normalization by row, column, or total, which makes the table readable when class sizes differ by orders of magnitude.
Applications
Confusion matrices have applications in a wide range of fields, including:
- Medical diagnostics and screening test evaluation
- Remote sensing and land cover classification accuracy assessment
- Fraud detection and intrusion detection system tuning
- Natural language processing tasks such as intent and sentiment classification
- Quality control and automated visual inspection
- Regulatory validation of machine learning systems, where per-class error must be documented