Judging category predictions.
Understand confusion matrices, precision, recall, thresholds, and probability quality.
Start with the meaning.
Classification chooses a category. In a binary task there are two categories, often called positive and negative. A confusion matrix counts four outcomes: true positives, true negatives, false positives, and false negatives. “True” means the prediction matches the known answer; “positive” names one class, not moral approval. If a system flags an unwanted message, a false positive is a useful message incorrectly flagged. A false negative is an unwanted message missed. The consequences differ.
Accuracy divides all correct predictions by all predictions. It is easy to read but can hide failure on a rare class. Precision asks: among the cases predicted positive, how many really were positive? Recall asks: among the actual positive cases, how many were found? Raising a threshold for declaring positive often improves precision and reduces recall; lowering it often does the reverse. Which direction is preferable depends on what a false alarm and a miss cost in the real task.
How the idea works.
Many classifiers first estimate a score or probability and then turn it into a class label using a threshold. A default threshold of 0.5 is a convention, not a universal optimum. If missing a dangerous condition is costly, a lower threshold may be appropriate, but it may increase false alarms. Threshold selection belongs on validation data, with final performance assessed on independent test data.
F1 is a combined score based on precision and recall. It is useful when both matter, but it hides which side of the tradeoff changed. ROC and precision-recall curves show behavior across thresholds; precision-recall views are often especially informative when the positive class is rare. Area under a curve summarises ranking ability, not the exact quality of a chosen threshold or the consequences of a specific decision.
Probability calibration is a separate question. A classifier can rank positive cases above negative cases while assigning probabilities that are too high or too low. If decisions rely on the probability number, compare predicted probability groups with observed frequencies. Also report results by meaningful subgroups. A single overall score may conceal that a model is accurate for one population and unreliable for another.
Follow the code.
from sklearn.metrics import confusion_matrix, precision_score, recall_score
actual = [1, 0, 1, 0, 1, 0]
predicted = [1, 1, 0, 0, 1, 0]
print(confusion_matrix(actual, predicted, labels=[0, 1]))
print(precision_score(actual, predicted))
print(recall_score(actual, predicted))The list actual holds six known answers; predicted holds six corresponding model decisions. With labels=[0, 1], the confusion matrix rows are actual classes and columns are predicted classes. The first row counts negatives: two correctly predicted 0 and one false alarm predicted 1. The second row counts positives: one missed positive and two correctly predicted 1. Precision is 2 out of 3 predicted positives, and recall is 2 out of 3 actual positives. Their equality here is an accident of these counts, not a general rule.
The example has no probability scores, so it cannot tell us how a different threshold would behave. It also has only six cases. A real evaluation would connect these counts to a known population and an explicit decision policy. The confusion matrix keeps the different error types visible in a way that one accuracy number cannot.
What the result does not say.
Multiclass classification has more than two categories. A confusion matrix then shows which categories are mistaken for which others. Precision and recall can be calculated per class and averaged in different ways; the averaging rule matters when class sizes differ.
A model may abstain or route uncertain cases to a person instead of forcing every input into a label. That changes the evaluation: report how often the model makes a decision and how good those decisions are. Sometimes the safest classification system is one that knows when to defer.
One clear idea.
Choose classification measures and thresholds from the real cost of false alarms and misses.
The ML cookbook
Complete, explained Python workflows sit alongside this reading course.