Classification · Part 1
Use class probabilities to make decisions, then see how maximum likelihood fits a logistic model.
We use categories every day. Can a model learn to assign them? Start with a hospital task.
Cholesterol measurement
Learns from labelled patients
0 Absent
1 Present
Each dot is one patient. Inspect a point, then compare the two classes and their sharp transition.
| Chapter | Question we will answer |
|---|---|
| 5.1 · A classification problem | What changes when the target is a category? |
| 5.2 · Probabilistic interpretation of machine learning | Why predict probabilities rather than certainty? |
| 5.3 · Optimal Bayes classifier | Which decision minimizes the chance of an error? |
| 5.4–5.5 · Naive Bayes / Estimating distributions | How can we estimate class probabilities from data? |
| 5.6 · Logistic regression | Can we model a class probability directly? |
| 5.6.1 · Maximum likelihood estimation | Which logistic coefficients make the observed labels most likely? |
| Patient i | Feature 1: cholesterol | Feature 2: age | Observed class |
|---|---|---|---|
| 1 | x₁₁ = 0.55 | x₁₂ = 50 | y₁ = 1 |
| 2 | x₂₁ = 0.20 | x₂₂ = 34 | y₂ = 0 |
Each possible label receives a probability.
| Case | Recorded cholesterol | Observed disease label |
|---|---|---|
| Patient i | xᵢ = 0.50 | yᵢ = 0 (absent) |
| Patient j | xⱼ = 0.50 | yⱼ = 1 (present) |
Uncertainty in the measurements that can be controlled, to a certain extent.
Better tools or additional predictors can decrease epistemic uncertainty.
Uncertainty intrinsic in the process that cannot be controlled and therefore not decreased.
We can know these probabilities and still be uncertain about the next outcome.
Suppose we record people’s hair colour during data collection. In this varied illustrative sample, we observe more people with black hair than with red hair.
Read: “The probability that the outcome is yᵢ, given input xᵢ.” Both expressions give that same probability.
Hypothetical patient input
Illustrative probabilities for this same input.
Probabilities change continuously. Often, optimizing them is simpler than optimizing discrete class labels.
If the observed class is 1, 0.60 to 0.80 strengthens the prediction while its label stays 1.
The Optimal Bayes Classifier (OBC) chooses the most probable class. It is a theoretical benchmark: in practice we usually have to estimate those probabilities.
Choose class 1.
It is the label with the higher chance of being right.
The lowest expected fraction of wrong labels, using these features and treating every mistake as equally costly.
| Candidate class c | 1 | 2 | 3 |
|---|---|---|---|
| True probability at this input | 0.10 | 0.70 | 0.20 |
The prediction is ŷᵢ = 2.
The largest probability is 0.70, but our output is the class label 2.
Binary example: p(1 | xᵢ) = 0.4.
The OBC predicts 0, but the observed label can still be 1, with probability 0.4.
This 0/1 outcome is called a Bernoulli outcome.
Even the most likely class can be wrong. To compare classifiers, we average their mistakes over possible inputs and outcomes.
Toy population: 10 equally likely cases
0 = correct label1 = wrong label
Reminder: the OBC chooses the class with the highest true probability. The individual case’s label is still unknown.
Return to two classes, {0,1}. Hold one input x fixed and compare the two possible predictions.
Here p(x) always means the probability of class 1. The class 1 is understood in this shorter notation.
Example at this input: suppose p(x) = 0.70.
We make a mistake when the actual class is 1.
We make a mistake when the actual class is 0.
We prove the two-class case. Let .
True labels: 0, 1, 1, 0
Predictions: 0, 1, 0, 0
Indicators: 1, 1, 0, 1
Three of four predictions are correct: 75%.
Weather example: the posterior is , the probability of rain given a cloudy sky.
How are these features distributed within class y?
How often is it cloudy when it rains?
How common is class y before seeing this input?
How common is rain before considering clouds?
How common is this input overall?
It normalizes the posterior.
Cloudy days among all days, rainy or dry.
| 100 emails | Contain the word “offer” | Do not contain it |
|---|---|---|
| 10 spam | 8 | 2 |
| 90 ordinary | 18 | 72 |
26 emails contain “offer”.
Only 8 of those are spam.
Evidence: “offer” occurs in 8 + 18 = 26 of all 100 emails, so .
Let . We leave out the observation index i to focus on one input.
n꜀ is the number of training observations with label c.
n is the total number of training observations.
Example: 10 spam emails out of 100 emails.
For this fixed input x, evaluate each candidate class c.
argmax returns the winning class label.
Replace each posterior with likelihood × prior / evidence.
The input x stays fixed while c changes.
We still need , the likelihood of the observed value of feature j within class y. To estimate it, group the training values by class first.
| Type of feature xⱼ | Method | Intuition |
|---|---|---|
| Quantitative | Parametric, such as a Gaussian | Choose a shape and estimate its parameters. |
| Quantitative | Nonparametric, such as kernel density estimation (KDE) | Estimate a smooth density from the observed values. |
| Categorical | Class-conditional proportions | Count how often each category occurs within each class. |
Species 0: 2.5, 3.5, 4, 4.5, 5.5 cm
Species 1: 4.5, 5.5, 6, 6.5, 7.5 cm
The average sets the center (4 or 6 cm). Average the squared distances from that center to estimate the variance (1 cm² in both groups here).
At 5.6 cm, each curve’s height gives for that species. Bayes’ rule combines these densities with the other features’ densities and the class prior.
One new flower has petal length x₁ and width x₂, measured in cm. Change its measurements and predict species 0 or 1.
First model the feature values within each class.
Estimate how the features are distributed for each class c and how common that class is (its prior).
Then use Bayes’ theorem to compute the probability of class c for the observed input xᵢ:
This combines the feature likelihood, class prior and evidence.
Model the probability of a class directly from the feature values.
For input xᵢ, calculate a weighted sum of its features: a linear score.
An S-shaped logistic function converts that score into a probability between 0 and 1:
The coefficients are learned from labelled examples. The next slide gives the formula for σ(xᵢ).
Set xᵢ₀=1 to include the intercept β₀.
σ(xᵢ) is the model’s class-1 probability.
The exponential keeps it between 0 and 1.
The class-0 probability is 1−σ(xᵢ).
Example: σ(xᵢ)=0.70.
The model assigns 70% to class 1 and 30% to class 0.
Since 0.70≥0.5, predict ŷᵢ=1.
If σ(xᵢ)=0.30 instead, predict ŷᵢ=0.
Move the midpoint and slope to change the curve. Compare the class-1 probability with the fixed 0.5 cutoff and read the resulting accuracy.
Why multiply? We want the probability of both recorded labels occurring together. Given the inputs, the model treats the two patients’ labels as independent, so it multiplies the probability it assigns to each recorded label (slide 25).
| Two fixed labels: y₁ = 1, y₂ = 0 | Model A | Model B |
|---|---|---|
| Model’s class-1 probabilities | (0.9, 0.9) | (0.6, 0.4) |
| Patient 1 has label 1: use σ(x₁) | 0.9 | 0.6 |
| Patient 2 has label 0: use 1−σ(x₂) | 1−0.9 = 0.1 | 1−0.4 = 0.6 |
| Joint likelihood: σ(x₁) × (1 − σ(x₂)) | 0.9 × 0.1 = 0.09 | 0.6 × 0.6 = 0.36 |
This is the probability assigned to what actually happened.
Example: σ(xᵢ)=0.8 gives a factor of 0.8. Increasing it toward 1 makes the observed label more likely.
Use the probability of class 0, because that is what happened.
Example: σ(xᵢ′)=0.2 gives 1−0.2=0.8. Moving σ(xᵢ′) toward 0 increases this factor.
Start with a candidate 𝛃.
Use the logistic formula to compute σ(xᵢ) for every training input.
For each label 1 use σ(xᵢ).
For each label 0 use 1−σ(xᵢ).
Multiply those contributions.
A numerical optimizer adjusts the coefficients to seek a larger likelihood.
Repeat until its stopping criterion is met.
The inputs and labels stay fixed. Select a patient, read the probability assigned to their observed label, then see how all 100 contributions combine.
Choose a worked example and describe what each step achieves.
Name every quantity in the corresponding formula, then explain why the formula does that job.
Lecture 11 · Classification · Part 1