01 / 21

Classification
Basics

Learn how models make binary predictions, measure success with the confusion matrix, and evaluate performance with accuracy, precision, and recall.

Business Analytics

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.

Input
Features describing each observation (e.g., age, income, purchase history)
Output
A predicted class label (e.g., "will churn" or "won't churn")

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.

Positive (+) Negative (−)

Why Classification Matters in Business

Marketing
Predict which customers will respond to a campaign to optimize ad spend
Finance
Flag fraudulent transactions in real time before they settle
Operations
Predict equipment failure before it happens to reduce downtime

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:

Actual Class Positive Negative Predicted Pos Neg True Positive (TP) False Positive (FP) False Negative (FN) True Negative (TN) ✓ Correct ✓ Correct FP and FN are errors

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

Accuracy
\(\dfrac{TP + TN}{TP + FP + FN + TN}\)
Fraction of all predictions that are correct
Precision
\(\dfrac{TP}{TP + FP}\)
Of all predicted positives, how many are truly positive?
Recall
\(\dfrac{TP}{TP + FN}\)
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.

Why harmonic mean?
It stays low unless both precision and recall are high. A model with 100% precision but 1% recall gets \(F_1 \approx 0.02\), not 50.5%.
When to use
Use \(F_1\) when false positives and false negatives are equally costly, or when you need a quick summary metric.

How to Apply Classification

1
Define the positive class. Decide which outcome is the "event of interest" (e.g., fraud, churn, disease).
2
Train the model. Feed labeled data to a classifier (logistic regression, decision tree, etc.).
3
Make predictions on test data. The model outputs a predicted label for each observation.
4
Build the confusion matrix. Compare predictions against actual labels and count TP, FP, FN, TN.
5
Compute metrics. Calculate accuracy, precision, recall, and \(F_1\) to assess performance.

Example: Email Spam Filter

A spam filter classifies 200 emails. Positive = Spam. Here are the results:

Actual Spam Not Spam Spam Not Predicted 40 TP 10 FP 5 FN 145 TN

Computing Metrics: Spam Filter

1
Accuracy = \(\frac{40 + 145}{40 + 10 + 5 + 145} = \frac{185}{200} = \) 92.5%
2
Precision = \(\frac{40}{40 + 10} = \frac{40}{50} = \) 80%
3
Recall = \(\frac{40}{40 + 5} = \frac{40}{45} \approx \) 88.9%
4
\(F_1\) = \(2 \cdot \frac{0.80 \times 0.889}{0.80 + 0.889} \approx \) 84.2%

Example: Fraud Detection

A bank screens 10,000 transactions. Only 50 are fraudulent (Positive = Fraud). The model flags 60 transactions as fraud:

Actual Fraud Legit Fraud Legit Predicted 45 TP 15 FP 5 FN 9935 TN

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.

Accuracy 99.5% Naive 99.8% Model Recall 0% Naive 90% Model Accuracy alone is not enough when classes are imbalanced.

Common Mistakes

✕ Only Reporting Accuracy
High accuracy can be meaningless with imbalanced classes. Always report precision, recall, and \(F_1\) alongside accuracy.
✕ Swapping FP and FN
A False Positive predicts positive when the truth is negative. A False Negative predicts negative when the truth is positive. The second word is the prediction.
✕ Ignoring Business Context
The "best" metric depends on the cost of errors. Maximizing precision is wrong when missing a real case (FN) is far more expensive than a false alarm (FP).

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?