Classification metrics
Метрики классификации
These measures tell you how well a model assigns items to categories. Some count the kinds of mistakes it makes at a chosen threshold; others measure how well its scores rank one class above another.
Also known as: precision, recall, F1, ROC-AUC
Start with the decision
A spam filter might give every email a score, with higher scores indicating that it looks more like spam. The threshold turns that score into an action: move the email to spam or leave it in the inbox. Before choosing a metric, ask what matters most: catching junk mail, protecting legitimate messages, or ranking emails for later review.
Read the confusion matrix
Call spam the positive class. A true positive (TP) is a spam email the filter flags. A false positive (FP) is a legitimate email it flags by mistake. A false negative (FN) is spam it misses, and a true negative (TN) is a legitimate message it leaves alone. At a fixed threshold, these four counts give you:
| Metric | Formula | What it tells you |
|---|---|---|
| Accuracy | (TP + TN) / (TP + TN + FP + FN) |
What fraction of all decisions were correct? |
| Precision | TP / (TP + FP) |
Of the emails flagged, how many were spam? |
| Recall | TP / (TP + FN) |
Of all spam emails, how many did the filter catch? |
| F1 | 2 × TP / (2 × TP + FP + FN) |
The harmonic mean of precision and recall, which stays low if either is low |
Worked example: high accuracy, missed spam
Suppose 20 of 1,000 emails are spam. The filter flags 15 spam messages and 10 legitimate ones, misses 5 spam messages, and leaves 970 legitimate ones alone. Precision is 15/25 = 60%, recall is 15/20 = 75%, and F1 is about 66.7%. Accuracy is 98.5%, but a filter that flags nothing still gets 98% accuracy. Its recall is zero, which shows how little accuracy alone tells you when positives are rare.
Evaluate the scores before picking a threshold
An ROC curve plots recall against the false positive rate, FP / (FP + TN), as the threshold changes. ROC-AUC is the area under that curve. It measures how often a randomly chosen positive gets a higher score than a randomly chosen negative, with ties counted as half. Compute it from scores, not from labels produced by a single cutoff, if you want to evaluate the model's ranking.
A good ROC-AUC does not guarantee good precision in production. When negatives vastly outnumber positives, even a small false positive rate can mean many false alarms. A precision–recall curve makes that trade-off visible; average precision (AP) summarizes precision across increases in recall.
What interviewers look for
Explain which error is more costly before naming a metric. If sending a legitimate message to spam is especially costly, you might maximize recall subject to a cap on false positives. Select the threshold on validation data and report results on a separate test set. For multiclass tasks, say how you average: macro gives each class equal weight, while weighted averaging gives more weight to classes with more examples. Report scores for important rare classes separately so they don't disappear into an average.