Lesson 13: Classification and Measuring Accuracy
Many data science problems are classification problems: sorting things into categories. Is this email spam or not? Will this student pass or fail? Which species is this penguin? A classifier takes the measurements and returns a label, and the question is how well it does. The ideas of Lesson 17 in the deep learning course apply here too, but this lesson builds them from the data science side, with the tools you already know.
The simplest classifier is a rule based on one measurement: if flipper length is above a threshold, call it species A, otherwise species B. That works when the groups separate cleanly. When they do not, a classifier can use several measurements at once — a logistic regression, for example, combines them into a score and turns the score into a probability. The details of the algorithm matter less at this stage than the evaluation: whatever the method, you must test it on data it did not train on, and report more than a single accuracy number.
Measuring a Classifier
Start with the confusion matrix: true positives, false positives, true negatives and false negatives. From it come precision — of the cases flagged, how many were real — and recall — of the real cases, how many were found. Accuracy is the share of all cases correct, but as you saw before, it misleads when the classes are uneven. A spam filter that is 99% accurate but lets the one important message through to the spam folder has failed its user, however good the number looks.
For classifiers that return probabilities, there is another useful picture: the ROC curve, which shows how the trade-off between catching real cases and raising false alarms changes as you move the threshold. A classifier that is no better than guessing hugs the diagonal; a good one bows towards the top-left corner. The area under the curve, or AUC, summarises that in one number — but like accuracy, it should be reported alongside the confusion matrix, not instead of it.
A Worked Example
Imagine a worked example: a model predicts whether a student will pass an exam, from attendance and homework scores. On the test set of 200 students, it gets 170 right — 85% accuracy. The confusion matrix shows it caught 60 of the 70 students who actually failed, but wrongly flagged 20 of the 130 who passed. Precision for "will fail" is 60 out of 80 flagged, or 75%; recall is 60 out of 70, or about 86%. If the model's job is to warn teachers early, that recall matters most — missing a struggling student costs more than a false alarm — and the report should say so, with the numbers to back it up.
Choosing a Threshold
A classifier's threshold is a decision about values, not only about mathematics. Setting it low catches more real cases but raises more false alarms; setting it high does the reverse. Which is right depends on what the mistakes cost. In a medical screening test, a missed case can be fatal, so the threshold is set low and false alarms are accepted. In a fraud system that freezes accounts, a false alarm can ruin someone's week, so the threshold is set higher and some fraud is accepted. There is no threshold that is right in the abstract — only one that is right for the costs of the two errors in this application.
That is why the people affected by a classifier should have a say in where its threshold sits. The data scientist can show the trade-off — the ROC curve is exactly that picture — but the choice of which point on the curve to use belongs to the people who bear the consequences. A model that is technically excellent and deployed with a threshold nobody agreed to is a failure of the whole project, not just of the model.
Key Terms
- Classification
- Sorting observations into categories, such as spam or not spam.
- Classifier
- A model that predicts a category label from measurements.
- Logistic regression
- A method that combines measurements into a score and turns it into a probability of belonging to a class.
- ROC curve
- A plot showing the trade-off between catching real cases and raising false alarms as the threshold moves.
- AUC
- Area under the ROC curve: a single number summarising a classifier's ranking ability.
- Threshold
- The cut-off on a model's score above which it predicts the positive class.
Discussion Questions
- A spam filter is 99% accurate. Why might it still be a bad filter?
- When would you prefer high recall even at the cost of low precision? When the reverse?
- A classifier returns probabilities. Why might you move its threshold away from 0.5?
Practice Questions
- What is classification?
- In the worked example, what is the model's recall for failing students?
- In the same example, what is its precision for 'will fail'?
- Why is accuracy alone a poor report for a classifier?
- What does an ROC curve show?
Show answers
- Sorting observations into categories — for example, spam or not spam, pass or fail, or which species a penguin is.
- About 86% — it caught 60 of the 70 students who actually failed.
- 75% — of the 80 students it flagged, 60 actually failed.
- Because it hides how the errors are shared out and can look excellent when the classes are uneven — for example, always guessing the common class.
- How the trade-off between catching real cases and raising false alarms changes as the classification threshold moves. A good classifier bows towards the top-left corner.