Machine LearningModule 5 · Guide and practice

Module 5 · Classification and logistic regression

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

Classification — original slide 3 of 4
Original slides · ClassificationOpen PDF ↗
Slide 3 of 4

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.

  • XX is the input, such as email features; xx is one particular input.
  • YY is the actual label. In a binary problem there are two labels, often 0 and 1.
  • pk(x)=P(Y=k∣X=x)p_k(x)=P(Y=k\mid X=x) is the probability of label kk, given input xx. The vertical bar means “given.”
  • C(x)C(x) is a classifier: the rule that chooses a label from the probabilities.

For two classes, the probabilities must add to 1:

p0(x)=1−p1(x).p_0(x)=1-p_1(x).

Read the slide’s data-generating example

The slide generates 100 examples by first drawing an input uniformly between −2-2 and 22, then drawing its label:

X∼Unif⁡(−2,2),p1(x)=11+e−2x.X\sim\operatorname{Unif}(-2,2), \qquad p_1(x)=\frac{1}{1+e^{-2x}}.

Uniform means equal-length input intervals are equally likely. e≈2.718e\approx2.718 is the exponential base. At input x=0x=0, the class-1 chance is 1/(1+e0)=1/21/(1+e^0)=1/2. At x=1x=1, it is 1/(1+e−2)≈0.8811/(1+e^{-2})\approx0.881.

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 XX. 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 xx.

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

Bayes Classifier and Local Averaging — original slide 2 of 5
Original slides · Bayes Classifier and Local AveragingOpen PDF ↗
Slide 2 of 5

“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 KK classes, the Bayes classifier is

C∗(x)=arg⁡max⁡k  pk(x).C^*(x)=\underset{k}{\arg\max}\;p_k(x).

KK 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 p1(x)≥0.5p_1(x)\geq0.5, 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 x≥0x\geq0 and class 0 for x<0x<0.

Unknown probabilities: estimate from nearby examples

Local averaging asks “among training inputs near this one, what fraction have label 1?” If N(x)\mathcal N(x) is a chosen neighborhood containing mm observations:

p^1(x)=1m∑i∈N(x)yi.\widehat p_1(x)=\frac{1}{m} \sum_{i\in\mathcal N(x)}y_i.

yiy_i is the observed 0-or-1 label. Adding these labels counts the ones; a hat marks an estimate from data.

Invented neighborhood: labels 1,1,0,1,01,1,0,1,0 give p^1(x)=3/5=0.6\widehat p_1(x)=3/5=0.6. Since 0.6≥0.50.6\geq0.5, 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

Logistic Regression — original slide 2 of 3
Original slides · Logistic RegressionOpen PDF ↗
Slide 2 of 3

First add contributions

For dd input features, make a score zz:

z=β0+∑j=1dβjxj.z=\beta_0+\sum_{j=1}^{d}\beta_jx_j.

β0\beta_0 is the intercept; βj\beta_j multiplies feature xjx_j. We use dd for the number of features so it cannot be confused with a probability pp. 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:

S(z)=11+e−z=ez1+ez.S(z)=\frac{1}{1+e^{-z}} =\frac{e^z}{1+e^z}.

Logistic regression assumes

p1(x)=S(z),p0(x)=1−S(z).p_1(x)=S(z),\qquad p_0(x)=1-S(z).
Score zzClass-1 probability S(z)S(z)With threshold 0.5
−2-20.1190.119Class 0
000.5000.500Tie; choose 1
220.8810.881Class 1

The threshold is the minimum probability needed to predict 1. With threshold 0.5, it is enough to check whether z≥0z\geq0. The decision boundary is where z=0z=0: the prediction switches labels there. Changing the threshold changes decisions without changing fitted probabilities.

Change the score or student inputs, then move the threshold to see when the predicted label changes.

What the coefficient actually changes

The odds of class 1 are its probability divided by the probability of class 0. For probability p=p1(x)p=p_1(x):

odds=p1−p=ez.\text{odds}=\frac{p}{1-p}=e^z. ln⁡ ⁣(p1−p)⏟log-odds, or logit=z.\underbrace{\ln\!\left(\frac{p}{1-p}\right)}_{\text{log-odds, or logit}}=z.

ln⁡\ln is the natural logarithm, which undoes the exponential. At p=0.8p=0.8, odds are 0.8/0.2=40.8/0.2=4: 4 to 1, not 4%.

Holding other inputs fixed, one extra unit of feature xjx_j adds βj\beta_j to the log-odds and multiplies the odds by eβje^{\beta_j}. It does not add βj\beta_j 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

Maximum Likelihood — original slide 2 of 5
Original slides · Maximum LikelihoodOpen PDF ↗
Slide 2 of 5

For each row, use the chance of what actually happened

Suppose a candidate model gives row ii class-1 probability pi=S(β0+∑jβjxij)p_i=S(\beta_0+\sum_j\beta_jx_{ij}). Here xijx_{ij} is feature jj of row ii.

  • If the actual label is yi=1y_i=1, use pip_i.
  • If the actual label is yi=0y_i=0, use 1−pi1-p_i.

For nn independent training examples, multiply these chances to get the likelihood:

L(β)=∏i=1npiyi(1−pi)1−yi.L(\boldsymbol\beta)= \prod_{i=1}^{n}p_i^{y_i}(1-p_i)^{1-y_i}.

∏\prod means multiply; β\boldsymbol\beta 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:

β^=arg⁡max⁡β  L(β).\widehat{\boldsymbol\beta} =\underset{\boldsymbol\beta}{\arg\max}\;L(\boldsymbol\beta).

Invented three-row comparison: actual labels are 1,0,11,0,1.

Candidate A proposes class-1 probabilities 0.8,0.3,0.60.8,0.3,0.6. The likelihood is 0.8(1−0.3)(0.6)=0.3360.8(1-0.3)(0.6)=0.336.

Candidate B proposes class-1 probabilities 0.6,0.4,0.50.6,0.4,0.5. The likelihood is 0.6(1−0.4)(0.5)=0.1800.6(1-0.4)(0.5)=0.180.

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 BB in dollars:

z=−10.65+0.0055B,p^1(B)=11+e−z.\begin{aligned} z&=-10.65+0.0055B,\\ \widehat p_1(B)&=\frac{1}{1+e^{-z}}. \end{aligned}
  • At the supplied B=1000B=1000: z=−10.65+0.0055(1000)=−5.15z=-10.65+0.0055(1000)=-5.15. Thus p^1=1/(1+e5.15)≈0.00577\widehat p_1=1/(1+e^{5.15})\approx0.00577, about 0.6%.
  • At the supplied B=2000B=2000: z=−10.65+0.0055(2000)=0.35z=-10.65+0.0055(2000)=0.35. Thus p^1=1/(1+e−0.35)≈0.5866\widehat p_1=1/(1+e^{-0.35})\approx0.5866, about 58.7% using these rounded coefficients.

5. Hypothesis tests: is a fitted weight clearly different from zero?

Hypothesis Testing in Logistic Regression — original slide 2 of 4
Original slides · Hypothesis Testing in Logistic RegressionOpen PDF ↗
Slide 2 of 4

Estimate ÷ uncertainty

The null hypothesis H0:βj=0H_0:\beta_j=0 says feature jj contributes nothing to this model’s score, with other inputs held fixed. The alternative is βj≠0\beta_j\neq0.

The fitted coefficient β^j\widehat\beta_j changes across samples. Its standard error, SE⁡(β^j)\operatorname{SE}(\widehat\beta_j), estimates the size of that variation. Measure the distance from zero in standard errors:

zj=β^j−0SE⁡(β^j).z_j=\frac{\widehat\beta_j-0} {\operatorname{SE}(\widehat\beta_j)}.

This z-statistic is different from the prediction score zz above. Under the null and suitable large-sample conditions, it is approximately standard normal. About 95% of that distribution lies between −2-2 and 22.

Use the module’s approximate rule: reject H0H_0 when ∣zj∣>2|z_j|>2; otherwise fail to reject. At exactly 2, this stated rule does not reject.

Invented fit: β^1=0.6\widehat\beta_1=0.6 and SE⁡=0.2\operatorname{SE}=0.2 give z1=0.6/0.2=3z_1=0.6/0.2=3. Since ∣3∣>2|3|>2, 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

Logistic Regression with More Classes — original slide 2 of 3
Original slides · Logistic Regression with More ClassesOpen PDF ↗
Slide 2 of 3

For KK classes and dd features, give each class kk its own weighted score:

zk=β0k+∑j=1dβjkxj.z_k=\beta_{0k}+\sum_{j=1}^{d}\beta_{jk}x_j.

β0k\beta_{0k} is class kk‘s intercept; βjk\beta_{jk} weights feature jj for that class. Turn the scores into positive weights ezke^{z_k}, then divide each weight by the total:

pk(x)=ezk∑ℓ=1Kezℓ.p_k(x)=\frac{e^{z_k}}{\sum_{\ell=1}^{K}e^{z_\ell}}.

This is softmax. The index ℓ\ell runs through every class in the denominator, so the probabilities sum to 1. There is no extra 1 when all KK classes are already in the sum. If a reference class has score 0, its weight is e0=1e^0=1; writing that term separately gives 1+1+ the sum over the remaining classes.

Invented scores: z1=0z_1=0, z2=ln⁡2z_2=\ln2, z3=ln⁡3z_3=\ln3. Exponentiating gives weights 1,2,31,2,3 and total 1+2+3=61+2+3=6:

(p1,p2,p3)=(16,26,36).(p_1,p_2,p_3)=\left(\frac16,\frac26,\frac36\right).

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)
Module 5 and 6 Practice Problems - Solutions — original slide 1 of 6
Original practice sheet · Module 5 and 6 Practice Problems - SolutionsOpen PDF ↗
Slide 1 of 6

Given: R(x)=1R(x)=1 on the inclusive interval [−2,2][-2,2], and 0 otherwise. Plot R(x)R(x) and R(x+1)R(x+1). Rule: horizontal shifts.

Ask which input reaches the “on” interval

RR outputs 1 when the number inside its parentheses is between −2-2 and 22.

For R(x)R(x), this is simply −2≤x≤2-2\leq x\leq2. For R(x+1)R(x+1), substitute the inside expression and subtract 1 throughout:

−2≤x+1≤2  ⟺  −3≤x≤1.\begin{aligned} -2\leq x+1\leq2 &\iff -3\leq x\leq1. \end{aligned}

So R(x+1)R(x+1) moves the rectangle left by 1. The height stays 1.

Rectangle before and after a left shift The upper plot is 1 from x equals minus 2 to 2, inclusive, and 0 outside. The lower plot is 1 from minus 3 to 1, inclusive, and 0 outside. Filled circles mark included endpoints at height 1; open circles at height 0 exclude those endpoints. R(x)R(x + 1) xx1100−22−31
Filled endpoints belong to the height-1 segment. Dashed guides show the jumps; they are not part of the function.

Check the new left endpoint: at x=−3x=-3, the inside is −3+1=−2-3+1=-2, so R(−3+1)=R(−2)=1R(-3+1)=R(-2)=1. At x=2x=2, the inside is 3, so R(2+1)=0R(2+1)=0.

Reusable rule: f(x+a)f(x+a) shifts the graph left by aa when a>0a>0; f(x−a)f(x-a) shifts it right by aa.

8. Shared practice Q2: three sigmoid identities

Given: S(x)=1/(1+e−x)S(x)=1/(1+e^{-x}). Rewrite it, evaluate S(−x)S(-x), and relate S(−x)S(-x) to 1−S(x)1-S(x). Rule: sigmoid identities.

1. Remove the negative exponent

Multiply numerator and denominator by exe^x. This multiplies the fraction by 1, and exe−x=e0=1e^xe^{-x}=e^0=1:

S(x)=11+e−xexex=exex+1.\begin{aligned} S(x)&=\frac{1}{1+e^{-x}}\frac{e^x}{e^x}\\ &=\frac{e^x}{e^x+1}. \end{aligned}

2. Replace every x with −x

Two minus signs cancel in the exponent. Then multiply numerator and denominator by e−xe^{-x} for the second form:

S(−x)=11+e−(−x)=11+ex=e−xe−x+1.\begin{aligned} S(-x)&=\frac{1}{1+e^{-(-x)}}\\ &=\frac{1}{1+e^x}\\ &=\frac{e^{-x}}{e^{-x}+1}. \end{aligned}

3. Subtract using a common denominator

Write 1=(1+e−x)/(1+e−x)1=(1+e^{-x})/(1+e^{-x}), then subtract:

1−S(x)=1+e−x−11+e−x=e−x1+e−x=S(−x).\begin{aligned} 1-S(x)&=\frac{1+e^{-x}-1}{1+e^{-x}}\\ &=\frac{e^{-x}}{1+e^{-x}}\\ &=S(-x). \end{aligned}

Meaning: reversing the score swaps the two class probabilities. For a numerical check at x=2x=2, S(2)≈0.8808S(2)\approx0.8808 and S(−2)≈0.1192S(-2)\approx0.1192; they add to 1.

9. Shared practice Q5: hours, GPA, and getting an A

Given: 100 students; X1X_1 is hours studied, X2X_2 is GPA, and Y=1Y=1 means receiving an A. The fitted coefficients are β^0=−6\widehat\beta_0=-6, β^1=0.05\widehat\beta_1=0.05, and β^2=1\widehat\beta_2=1.

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 X1=40X_1=40 and GPA X2=3.5X_2=3.5:

z=β^0+β^1X1+β^2X2=−6+0.05(40)+1(3.5)=−6+2+3.5=−0.5.\begin{aligned} z&=\widehat\beta_0+\widehat\beta_1X_1 +\widehat\beta_2X_2\\ &=-6+0.05(40)+1(3.5)\\ &=-6+2+3.5=-0.5. \end{aligned}

Rule: put the score into the sigmoid. Using the calculated z=−0.5z=-0.5:

p^1=11+e−z=11+e0.5≈0.3775≈37.8%.\begin{aligned} \widehat p_1&=\frac{1}{1+e^{-z}}\\ &=\frac{1}{1+e^{0.5}}\\ &\approx0.3775\approx\boxed{37.8\%}. \end{aligned}

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 ln⁡(0.5/(1−0.5))=ln⁡1=0\ln(0.5/(1-0.5))=\ln1=0. Let hh be the required hours. Keep the supplied GPA at 3.5:

0=−6+0.05h+1(3.5),0.05h=6−3.5=2.5,h=2.50.05=50 hours.\begin{aligned} 0&=-6+0.05h+1(3.5),\\ 0.05h&=6-3.5=2.5,\\ h&=\frac{2.5}{0.05}=\boxed{50\text{ hours}}. \end{aligned}

Check: −6+0.05(50)+3.5=0-6+0.05(50)+3.5=0, and S(0)=1/2S(0)=1/2. 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 q=P(Y=1∣X=x)q=P(Y=1\mid X=x) and let z=β0+∑j=1dβjxjz=\beta_0+\sum_{j=1}^{d}\beta_jx_j be the weighted score. We use qq for the probability here; the question uses p(X)p(X). “Log” in this identity means the natural logarithm.

Log-odds → probability

Start from ln⁡(q/(1−q))=z\ln(q/(1-q))=z, with 0<q<10<q<1. Exponentiate both sides because eln⁡a=ae^{\ln a}=a for a>0a>0:

q1−q=ez.\frac{q}{1-q}=e^z.

Multiply by 1−q1-q and collect every term containing qq:

q=ez(1−q),q=ez−ezq,q+ezq=ez,q(1+ez)=ez.\begin{aligned} q&=e^z(1-q),\\ q&=e^z-e^zq,\\ q+e^zq&=e^z,\\ q(1+e^z)&=e^z. \end{aligned}

Divide by the positive quantity 1+ez1+e^z:

q=ez1+ez.\boxed{q=\frac{e^z}{1+e^z}}.

Substituting z=β0+∑jβjxjz=\beta_0+\sum_j\beta_jx_j gives the requested logistic formula.

Probability → log-odds

Starting with q=ez/(1+ez)q=e^z/(1+e^z), the complementary probability is 1−q=1/(1+ez)1-q=1/(1+e^z). Divide the two, then take the natural log:

q1−q=ez/(1+ez)1/(1+ez)=ez,ln⁡ ⁣(q1−q)=z.\begin{aligned} \frac{q}{1-q} &=\frac{e^z/(1+e^z)}{1/(1+e^z)}=e^z,\\ \ln\!\left(\frac{q}{1-q}\right)&=z. \end{aligned}

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.

Module 5 formulas

Open the formula sheet →