Classification means choosing a label. First estimate how likely each label is; then choose one. Logistic regression builds those probabilities from the same weighted sums used in regression.
Slides → small steps → practice. Browse the original slides above each topic, or click to enlarge. Keep the formula sheet nearby. This module covers shared practice Q1, Q2, Q5, and Q6; Module 6 covers Q3, Q4, and Q7.
1. A label and a probability are different things
Same inputs can lead to different labels
An email is spam or not spam; it cannot be 0.7 spam. But a model can give it a 70% chance of being spam.
- is the input, such as email features; is one particular input.
- is the actual label. In a binary problem there are two labels, often 0 and 1.
- is the probability of label , given input . The vertical bar means “given.”
- is a classifier: the rule that chooses a label from the probabilities.
For two classes, the probabilities must add to 1:
Read the slide’s data-generating example
The slide generates 100 examples by first drawing an input uniformly between and , then drawing its label:
Uniform means equal-length input intervals are equally likely. is the exponential base. At input , the class-1 chance is . At , it is .
The observed label is still 0 or 1. The smooth curve describes the chance of getting 1 at each input, not a halfway label.
Decode PDF, PMF, and conditional
A PDF is a probability density for a continuous quantity such as . Probabilities are areas over intervals; the height at one point is not itself a probability. A PMF assigns probabilities to discrete values, such as labels 0 and 1. A conditional probability uses information already known: here, the input .
The slide’s sampling recipe is “draw an input, then draw a label using that input’s class probabilities.” Module 6 builds a classifier by modeling the input distribution within each class.
2. Bayes: pick the most likely label
“Optimal” means the smallest possible error rate
If a label has probability 0.8, choosing it gives a 0.2 chance of being wrong. Choosing the other label gives a 0.8 chance of being wrong. Pick the biggest probability to minimize mistakes, when all mistakes count equally.
For classes, the Bayes classifier is
counts labels. Arg max means “the label that gives the largest value.” The star marks the ideal rule using the true probabilities.
For labels 0 and 1, choose 1 when , and 0 otherwise. At exactly 0.5, either label has the same error; we choose 1 to settle the tie. In the slide’s example, this gives class 1 for and class 0 for .
Unknown probabilities: estimate from nearby examples
Local averaging asks “among training inputs near this one, what fraction have label 1?” If is a chosen neighborhood containing observations:
is the observed 0-or-1 label. Adding these labels counts the ones; a hat marks an estimate from data.
Invented neighborhood: labels give . Since , predict 1.
Curse of dimensionality: with many input features, examples spread out in many directions. A tiny neighborhood may contain almost nobody; a big one includes less similar inputs. Logistic regression addresses this by assuming a specific shape for the probability rule.
3. Logistic regression: score → probability → label
First add contributions
For input features, make a score :
is the intercept; multiplies feature . We use for the number of features so it cannot be confused with a probability . The score can be any real number, so it is not yet a probability.
Then squash the score between 0 and 1
The sigmoid, or logistic function, is an S-shaped converter:
Logistic regression assumes
| Score | Class-1 probability | With threshold 0.5 |
|---|---|---|
| Class 0 | ||
| Tie; choose 1 | ||
| Class 1 |
The threshold is the minimum probability needed to predict 1. With threshold 0.5, it is enough to check whether . The decision boundary is where : the prediction switches labels there. Changing the threshold changes decisions without changing fitted probabilities.
What the coefficient actually changes
The odds of class 1 are its probability divided by the probability of class 0. For probability :
is the natural logarithm, which undoes the exponential. At , odds are : 4 to 1, not 4%.
Holding other inputs fixed, one extra unit of feature adds to the log-odds and multiplies the odds by . It does not add to the probability. A positive coefficient raises the class-1 probability; a negative one lowers it.
4. Maximum likelihood: reward probabilities assigned to actual labels
For each row, use the chance of what actually happened
Suppose a candidate model gives row class-1 probability . Here is feature of row .
- If the actual label is , use .
- If the actual label is , use .
For independent training examples, multiply these chances to get the likelihood:
means multiply; means all the coefficients together. The exponents select the correct chance: an exponent of 0 turns the unused factor into 1.
Maximum likelihood estimation (MLE) chooses coefficients that make this product as large as possible:
Invented three-row comparison: actual labels are .
Candidate A proposes class-1 probabilities . The likelihood is .
Candidate B proposes class-1 probabilities . The likelihood is .
Candidate A assigns more probability to the observed combination of labels. Fitting searches for the best coefficients, not just the better of these two candidates. The likelihood scores the observed labels for chosen coefficients; it is not the probability that those coefficients are true.
After fitting, substitute into the score
The slide’s fitted default model uses balance in dollars:
- At the supplied : . Thus , about 0.6%.
- At the supplied : . Thus , about 58.7% using these rounded coefficients.
5. Hypothesis tests: is a fitted weight clearly different from zero?
Estimate ÷ uncertainty
The null hypothesis says feature contributes nothing to this model’s score, with other inputs held fixed. The alternative is .
The fitted coefficient changes across samples. Its standard error, , estimates the size of that variation. Measure the distance from zero in standard errors:
This z-statistic is different from the prediction score above. Under the null and suitable large-sample conditions, it is approximately standard normal. About 95% of that distribution lies between and .
Use the module’s approximate rule: reject when ; otherwise fail to reject. At exactly 2, this stated rule does not reject.
Invented fit: and give . Since , reject a zero coefficient. This is evidence of association in the model; it does not establish causation. Failing to reject does not prove the coefficient is zero.
With multiple features, add all their contributions to the score, fit all coefficients by MLE, and test each coefficient relative to its own standard error.
6. More than two labels: give each a share of the total
For classes and features, give each class its own weighted score:
is class ‘s intercept; weights feature for that class. Turn the scores into positive weights , then divide each weight by the total:
This is softmax. The index runs through every class in the denominator, so the probabilities sum to 1. There is no extra 1 when all classes are already in the sum. If a reference class has score 0, its weight is ; writing that term separately gives the sum over the remaining classes.
Invented scores: , , . Exponentiating gives weights and total :
Choose class 3, the largest probability. MLE fits the coefficients by maximizing the product of the probabilities assigned to the actual training labels.
7. Shared practice Q1: moving the rectangle
View the original practice sheet (includes solutions)
Given: on the inclusive interval , and 0 otherwise. Plot and . Rule: horizontal shifts.
Ask which input reaches the “on” interval
outputs 1 when the number inside its parentheses is between and .
For , this is simply . For , substitute the inside expression and subtract 1 throughout:
So moves the rectangle left by 1. The height stays 1.
Check the new left endpoint: at , the inside is , so . At , the inside is 3, so .
Reusable rule: shifts the graph left by when ; shifts it right by .
8. Shared practice Q2: three sigmoid identities
Given: . Rewrite it, evaluate , and relate to . Rule: sigmoid identities.
1. Remove the negative exponent
Multiply numerator and denominator by . This multiplies the fraction by 1, and :
2. Replace every x with −x
Two minus signs cancel in the exponent. Then multiply numerator and denominator by for the second form:
3. Subtract using a common denominator
Write , then subtract:
Meaning: reversing the score swaps the two class probabilities. For a numerical check at , and ; they add to 1.
9. Shared practice Q5: hours, GPA, and getting an A
Given: 100 students; is hours studied, is GPA, and means receiving an A. The fitted coefficients are , , and .
Rules: logistic prediction and solving for a target probability.
1. Find the chance at 40 hours and GPA 3.5
Rule: add weighted inputs. With supplied hours and GPA :
Rule: put the score into the sigmoid. Using the calculated :
The 100 students were used for fitting; you do not divide this prediction by 100.
2. Find hours for a 50% chance, with GPA still 3.5
Rule: a 50% probability means score 0, since . Let be the required hours. Keep the supplied GPA at 3.5:
Check: , and . This is 50 total hours, 10 more than the student’s original 40, according to the fitted model.
10. Shared practice Q6: log-odds and the sigmoid are the same rule
Given: prove that the linear log-odds form and the logistic probability form are equivalent. Rule: odds and logit.
Let and let be the weighted score. We use for the probability here; the question uses . “Log” in this identity means the natural logarithm.
Log-odds → probability
Start from , with . Exponentiate both sides because for :
Multiply by and collect every term containing :
Divide by the positive quantity :
Substituting gives the requested logistic formula.
Probability → log-odds
Starting with , the complementary probability is . Divide the two, then take the natural log:
Same model, two directions: use the sigmoid to turn a score into a probability; use the logit to turn a target probability back into a score.
Continue to Module 6: Bayes’ rule and discriminant analysis for shared Q3, Q4, and Q7.