ContentsChapter 5 · Classification1 of 3

5.1

Logistic regression

In this chapter
  1. 5.1Logistic regression
  2. 5.2Naive Bayes
  3. 5.3Evaluation metrics
Before you readA spam filter gives a message a score of −2-2 How can it turn that score into a probability?
Watch: Logistic regression · StatQuest with Josh Starmer

A classifier predicts a label. Use y=1y=1 for spam and y=0y=0 for not spam. Logistic regression first takes a dot product to give the message a score z=wTx+bz=w^Tx+b The sigmoid turns any real score into a probability:

p=P(y=1∣x)=σ(z)=11+e−z.p=P(y=1\mid x)=\sigma(z)=\frac{1}{1+e^{-z}}.

A score of zero gives p=12p=\frac12 Positive scores push pp towards 1; negative scores push it towards 0. The score is also the log-odds: log⁡p1−p=z\log\frac{p}{1-p}=z Adding one to the score multiplies the odds by ee

00.51-6-4-20246
z=x+bz=x+b = -1pp = 0.269Decision boundary: xx = 2Prediction: not spam
Move the feature to follow the sigmoid. Change the bias to move the decision boundary: the dashed guides meet at probability 0.5.

If you predict spam whenever p≥0.5p\geq 0.5 the decision boundary is where wTx+b=0w^Tx+b=0 With two features that is a straight line (for non-zero ww The sigmoid bends the probability curve, not the boundary in feature space.

To learn ww and bb give each observed label as much probability as possible. The model gives label 1 probability pp and label 0 probability 1−p1-p Maximising the product of these probabilities across training examples is equivalent to minimising the sum of their binary cross-entropy losses:

ℓ(y,p)=−ylog⁡p−(1−y)log⁡(1−p).\ell(y,p)=-y\log p-(1-y)\log(1-p).

For a spam message, the loss is −log⁡p-\log p If the model confidently calls it not spam (pp near zero), that loss is large.

Recall
At threshold 0.50.5 what equation defines the decision boundary?
Recall
What is the loss when the observed label is 00
Exercise
For w=(2,−1)Tw=(2,-1)^T x=(1,3)Tx=(1,3)^T and b=0b=0 find the score and the predicted label at threshold 0.50.5 Give the two numbers in that order.
Exercise
The model gives two messages spam probabilities 0.80.8 and 0.30.3 The first is spam and the second is not. What probability does the model give to both labels occurring together, treating the messages as independent?