Week 9 · Perceptron and logistic regression
Read the words of the video
- Imagine you have red and green marbles scattered on a table. Your goal is to find a straight stick that you can place so all the red marbles are on one side and all the green marbles are on the other. This stick helps us separate things. This is exactly what a linear classifier does!
- The Perceptron is like a simple teacher. It nudges the stick only when a marble is on the wrong side. Logistic Regression is gentler; it nudges the stick a little for every marble, based on how wrong its probability guess was. Both try to find the best straight stick to separate our data.
- Let's try it! Start with the stick anywhere. Pick a red marble on the green side. Nudge the stick just enough to bring that red marble to its side. Repeat this for all marbles that are on the wrong side. Keep going until no marbles are misplaced. The stick has learned to separate them!
- For your exam, remember the Perceptron updates only on mistakes, and only works if data is clearly separable. Logistic Regression always finds a solution, outputting probabilities. Both give a straight line boundary! Keep practicing, and you'll soon be an expert at separating data with your clever stick!
The one-minute story
Spread red and green marbles on a table and try to lay a straight stick between them so all the reds are on one side and all the greens on the other. That stick is a linear classifier, and the whole job is to find it.
The perceptron is a very simple teacher. Start with any stick. Pick up one marble at a time; if it is on the wrong side, nudge the stick a little towards the right side for that marble. Keep going round until nobody is on the wrong side. If a straight stick can separate the marbles, this is guaranteed to work.
Logistic regression is a gentler teacher. Instead of a hard "this side / that side", it gives a probability: far on the red side means "99% red", right on the stick means "50/50". Instead of nudging only on mistakes, it nudges a little for every marble, in proportion to how wrong the probability was.
Both are discriminative: they learn the boundary directly and never model what the marbles look like.
By the end of this week you can:
- describe a linear classifier and its decision boundary;
- run the perceptron algorithm by hand and state its convergence theorem;
- write the logistic regression model, loss and gradient;
- compare the perceptron and logistic regression.
Words you need
- Linear classifier - predicts by the sign of .
- Decision boundary - the set of points where : a line in 2-D, a plane in 3-D.
- Linearly separable - some line separates the two classes perfectly.
- Label convention - the perceptron uses ; logistic regression uses .
- Sigmoid - squashes any number into .
- Log-odds (logit) - ; for logistic regression it equals .
Idea 1: Linear classifiers
Predict . The weight vector is perpendicular to the boundary and points towards the positive side. The value says which side and, divided by , how far from the boundary.
A trick to avoid carrying around: add a constant feature 1 to every and absorb into . Then the prediction is just .
A point with is classified correctly exactly when .
Idea 2: The perceptron algorithm
Perceptron (labels , bias absorbed):
- Start with .
- Go through the training examples. For each : if (a mistake), update
- Repeat passes over the data until a full pass makes no mistakes.
Why does the update help? After the update the new score for that same example is , which is larger by . So the mistake gets less wrong (it may still need more nudges).
Convergence theorem. If the data is linearly separable with margin (every point is at least from some separating boundary, with ) and every , then the perceptron makes at most mistakes and stops. If the data is not linearly separable (the classic example: XOR), it never converges, cycling forever. Also, the solution it finds is not unique: it depends on the order of the data.
Question. Data: with ; with ; with (bias already absorbed). Run the perceptron from , going through the points in this order.
Solution.
Pass 1.
- : mistake. .
- : , correct.
- : , mistake. .
Pass 2.
- : ✓. : , times is ✓. : ✓.
No mistakes: stop with after two updates. The boundary is .
Idea 3: Logistic regression
Instead of a hard sign, model the probability of class 1:
Equivalently, the log-odds are linear: . The class is predicted as 1 when , i.e. , so the decision boundary is still the straight line .
Training by maximum likelihood. With labels the likelihood of the data is with . Taking the negative log gives the cross-entropy (log) loss:
It is convex with a lovely gradient:
("prediction minus truth, times the input"). There is no closed-form solution, so use gradient descent: .
Extras. If the classes are perfectly separable the likelihood keeps improving as , so add a regulariser (as in ridge) to keep finite. For more than two classes, replace the sigmoid with softmax (week 12).
Question. Two examples (no bias): with and with . Start with and use . Do one step.
Solution. With , every , so .
The weights moved so that gets a higher score and a lower one. ✓
Question. , bias . What is at ?
Solution. . . Predict class 1.
Idea 4: Perceptron versus logistic regression
| Perceptron | Logistic regression | |
|---|---|---|
| Output | hard label | probability in |
| Updates | only on mistakes | on every example, scaled by the error |
| Loss | mistake-driven (0-1 flavoured) | log-loss (smooth, convex) |
| Convergence | only if linearly separable | always fine (with regularisation) |
| Boundary | a line | a line |
Your turn
Start from . Examples in order: with , then with . Apply one pass of the perceptron.
Need a hint?
Update only when .
Show the full solution
First: , mistake: . Second: , , mistake: .
A model has and . Compute the class-1 probability at and say which side of the boundary it is on.
Need a hint?
.
Show the full solution
, so the positive side. .
For and , write the decision boundary and give two points on it.
Need a hint?
Set .
Show the full solution
. E.g. and . is perpendicular to this line.
(a) The perceptron converges on any data set. (b) Logistic regression has a closed-form solution. (c) The logistic regression boundary is linear. (d) The log-loss is convex in .
Need a hint?
Think about XOR and the gradient formula.
Show the full solution
(a) False, only if separable. (b) False, use gradient descent. (c) True. (d) True.
The data have and are separable with margin . Give the perceptron's mistake bound.
Need a hint?
.
Show the full solution
mistakes at most. A bigger margin means fewer.
Exam radar
In the End Term, summaries mention logistic regression in 23 papers, perceptron in 13 and perceptron algorithm in 15, weight update in 13, decision boundary in 19, sigmoid in 10.
Typical: "perform one update of the perceptron", "when does the perceptron converge?", the gradient of the log-loss ( times ), "what is the decision boundary of logistic regression?" (linear), the probability computed from given weights, and a comparison table like the one above.
- Perceptron labels are ; logistic labels are . The gradient formula assumes .
- Update only on a mistake (including the "score " case).
- Don't forget the bias: add a feature of 1.
- The perceptron solution is not unique; logistic regression's is (when regularised).
- Linearly separable is required for perceptron convergence, not for logistic regression to run.
Quick quiz
The perceptron update on a mistake is...
- w minus y x
- w plus y x
- w times y
- w plus x
The decision boundary of logistic regression is...
- a circle
- a straight line (hyperplane)
- a parabola
- not defined
The gradient of the logistic log-loss for labels in {0,1} is...
- sum of (y - p) squared
- sum of (p - y) x
- sum of p x
- sum of y x
Select all TRUE statements. (select all that apply)
- The perceptron stops only if the data is linearly separable.
- Logistic regression outputs a probability.
- Logistic regression has a closed-form solution.
- The perceptron minimises the log-loss.
Recap
- Linear classifier: ; boundary; correct iff .
- Perceptron: on a mistake ; converges iff separable; mistake bound ; solution not unique.
- Logistic regression: ; log-odds linear; boundary linear; cross-entropy loss, gradient ; gradient descent; regularise if separable.
- Perceptron: mistake if ; update ; mistakes
- Logistic: ;
- Loss ; gradient ; step
- MLF week 7 - Finding the lowest point: gradient descent: Training the perceptron and logistic regression is gradient descent.
- MLF week 9 - Optimising with rules: Lagrange and KKT: Logistic regression written as an optimisation problem.
- MLF week 11 - The exponential family: Logistic regression is a member of the exponential family of models.
- MLP week 5 - Logistic regression in sklearn: Logistic regression: the theory, then the code.
- MLP week 6 - Binary classifiers and how to judge them: Binary classifiers and their metrics.
Practise with real exam questions
These come straight from past papers (with the real figures and the official answer key). Open 39 real questions on this week All End Term papers