The Perceptron Algorithm
The oldest learning algorithm still in use. It looks at one example at a time, does nothing when it is right, and nudges its weights toward the answer when it is wrong. If the data can be separated by a straight line, it is guaranteed to find one.
Prerequisites: The Supervised Learning Framework, Linear Algebra for Quants
Most model fitting starts by writing down a loss over the whole dataset and then minimising it. The perceptron does not. It walks through the examples one at a time, makes a call on each, and only touches its weights when it gets one wrong — and even then it makes the smallest sensible correction, aimed squarely at the example that just embarrassed it. There is no loss surface to picture and no matrix to invert. It is the shortest path from "here is some labelled data" to "here is a classifier", and everything from stochastic gradient descent to neural networks inherits its shape.
Think of a trainee marking each trade as good or bad before the outcome is known. Their supervisor says nothing when the call is right. When it is wrong, the supervisor points at the trade and says "that one — lean more toward its features next time". The trainee does not rewrite their whole framework; they shift it slightly in the direction of the example they missed. Repeat, and the calls stop being wrong.
The rule
The model is a weighted score. Each feature gets a weight saying how much it counts, and a bias shifts the whole thing up or down. The prediction is the sign of
Positive means class , negative means class . So the boundary is the flat surface where the score is exactly zero, and is the arrow pointing perpendicular to it, toward the positive side.
Labels are written or rather than 1 and 0 for one reason: the single quantity then tells you everything. It is positive when the call was right and negative when it was wrong. The algorithm is one line:
In plain English: if this example is on the wrong side, add its own feature vector to the weights when it should have been positive, and subtract it when it should have been negative. Nothing happens on correct predictions.
Why does that help? Recompute the score for the same example straight after an update:
In plain English: the update moves that example's score in the right direction by an amount equal to its own squared length plus one. It might not be enough to flip the call in one go, but it never moves the wrong way.
Worked example 1: three updates by hand
Four labelled examples in two dimensions:
| point | ||
|---|---|---|
| A | ||
| B | ||
| C | ||
| D |
Start at , , and treat a score of exactly zero as a mistake.
A. Score , so : mistake. Add to the weights and to the bias. Now , .
B. Score , so another mistake. Subtract and subtract 1: , .
C. Score : mistake again. Subtract and subtract 1: , .
D. Score , and , so . Correct. No update.
Now run a second pass with the same weights and check all four: A scores (correct), B scores (correct, since ), C scores (correct), D scores 4 (correct). A full sweep with no mistakes means the algorithm has stopped. The final rule is , found in three updates and six passes' worth of arithmetic you can do on a napkin.
Worked example 2: how many mistakes can it make?
The convergence guarantee has two ingredients. is the length of the longest example — how far out the data reaches. is the margin: fold the bias into the features by appending a 1 to every , find the unit-length weight vector that separates the data best, and is the smallest score it gives any example. The Perceptron Convergence Theorem then says the algorithm makes at most mistakes, no matter how the examples are ordered.
For the four points above, the augmented vectors are , , and , with lengths , , , . So and .
Use the separator the algorithm actually found, , whose length is . Its raw scores on the four points are 4, 4, 3, 4, so the smallest is 3 and the margin is . The mistake bound is
so at most seven mistakes. We made three. The bound is honest but loose, which is typical — and notice what is not in it: the number of features, and the number of examples. A perceptron on a million rows in a thousand dimensions makes the same bounded number of mistakes, provided the margin holds up.
Wide margin means few mistakes; the bound depends on the ratio of the data's reach to the width of the gap between classes, squared. This is the same quantity that Support Vector Machines deliberately maximise — the perceptron just settles for any separator it stumbles into.
What this means in practice
The perceptron is rarely the model you ship, but its shape is everywhere.
It is online: it consumes one example, updates, and forgets it. That suits a live signal that must absorb today's fill without refitting on ten years of history, and it is the ancestor of every Stochastic Gradient Descent variant, which is the same "look at a bit of data, take a step" idea with a proper loss attached.
It finds a separator, not the best one. Where an SVM picks the widest corridor, the perceptron stops the moment nothing is wrong, so its boundary can sit right up against a training point and generalise poorly. The averaged perceptron — keep a running mean of every weight vector you passed through and predict with that — recovers much of the lost stability for almost no extra code.
Its output is a signed distance with no calibration behind it. A score of 4 is not "80 percent confident"; feeding it into a sizing rule as though it were a probability is a real and common error. Use Logistic Regression when you need probabilities.
If the data is not linearly separable, the perceptron never stops. It cycles forever, and worse, it does not converge to anything sensible on the way — a handful of unseparable outliers can leave the weights swinging between wildly different boundaries, so whichever example you happen to stop on decides your model. Always cap the number of passes, and always keep either the averaged weights or the best-scoring weights seen so far rather than the last ones.
Related concepts
Practice in interviews
Further reading
- Rosenblatt, The Perceptron: A Probabilistic Model for Information Storage (1958)
- Novikoff, On Convergence Proofs for Perceptrons (1962)
- Shalev-Shwartz & Ben-David, Understanding Machine Learning (Ch. 9)