Classification
Basics
Learn how models make binary predictions, measure success with the confusion matrix, and evaluate performance with accuracy, precision, and recall.
What is Classification?
Classification is a type of supervised machine learning where the model learns to assign each input to one of a set of discrete categories (called classes). Unlike regression, which predicts a continuous number, classification predicts a label.
Binary Outcomes
The simplest and most common form is binary classification: every observation belongs to exactly one of two classes. We typically call these the Positive class and the Negative class.
Why Classification Matters in Business
Predictions vs. Reality
Every prediction a classifier makes falls into one of four categories based on whether the prediction was correct and what the true label was:
The Confusion Matrix
The confusion matrix is a \(2 \times 2\) table that organizes all predictions by comparing the predicted label against the actual label. It is the foundation for every classification metric.
Key Metrics from the Matrix
Fraction of all predictions that are correct
Of all predicted positives, how many are truly positive?
Of all actual positives, how many did we catch?
Precision vs. Recall
| Precision | Recall | |
|---|---|---|
| Question answered | When I say "yes," am I right? | Did I find all the real "yes" cases? |
| Numerator | TP | TP |
| Denominator | TP + FP | TP + FN |
| Penalizes | False Positives | False Negatives |
| Prioritize when | False alarms are costly (spam filter) | Missing cases is costly (disease screening) |
The \(F_1\) Score
When you need a single number that balances both precision and recall, use the \(F_1\) score: the harmonic mean of the two.
How to Apply Classification
Example: Email Spam Filter
A spam filter classifies 200 emails. Positive = Spam. Here are the results:
Computing Metrics: Spam Filter
Example: Fraud Detection
A bank screens 10,000 transactions. Only 50 are fraudulent (Positive = Fraud). The model flags 60 transactions as fraud:
The Accuracy Trap
In the fraud example, a model that predicts "legitimate" for every transaction achieves \(\frac{9950}{10000} = 99.5\%\) accuracy, but catches zero fraud. This is the accuracy paradox.
Common Mistakes
Metric Summary
| Metric | Formula | Best For |
|---|---|---|
| Accuracy | \(\frac{TP+TN}{Total}\) | Balanced classes |
| Precision | \(\frac{TP}{TP+FP}\) | Minimizing false alarms |
| Recall | \(\frac{TP}{TP+FN}\) | Catching all positives |
| \(F_1\) Score | \(\frac{2 \cdot P \cdot R}{P + R}\) | Balancing precision and recall |
Question 1 of 5
A model predicts "Fraud" for a transaction that is actually legitimate. What type of outcome is this?
Question 2 of 5
A confusion matrix shows: TP = 30, FP = 20, FN = 10, TN = 140. What is the precision?
Question 3 of 5
A disease screening test has high recall but low precision. What does this mean in practice?
Question 4 of 5
In a dataset of 10,000 emails with only 50 spam, a model predicts "Not Spam" for every email. What is its accuracy?
Question 5 of 5
Which metric is the harmonic mean of precision and recall?