Foundations to Frontier
Tests
D. Classical Machine Learning › D2 Linear Classifiers

The Perceptron

The perceptron is the first learning algorithm for a linear classifier: predict the sign of w·x + b and, whenever a training point is misclassified, add y·x to the weights. On linearly separable data it is guaranteed to stop after at most (R/γ)² mistakes (Novikoff's theorem); on non-separable data it never settles. This page covers the algorithm, its geometry, the convergence proof, the XOR limitation that stalled neural-network research, and the practical fixes: the averaged (voted) perceptron, the pocket algorithm and margins. In the exercises you implement the perceptron, verify the mistake bound and the XOR failure; then you build a lander hazard detector and compare the last-iterate, averaged and pocket perceptrons under label noise, and measure how loose Novikoff's bound is in practice.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Learn only from your mistakes

Draw a line that separates red points from blue ones. Start with any line. Visit the points one by one. If a point is on the correct side, do nothing. If it is on the wrong side, nudge the line toward putting it right. That is the whole perceptron algorithm (Rosenblatt, 1958), the ancestor of every neural network. If a separating line exists, the nudging is guaranteed to stop. If none exists, it never does.

Perceptron: learn from each mistake
mistake 2 in epoch 1 · w = (1.07, 0.56), b = 0 · training accuracy: current 100%, averaged 63% · total mistakes in 25 epochs: 2
Solid line: the current boundary after this mistake (the ringed point). Dashed green: the averaged perceptron. With 0% flipped labels the mistakes stop (convergence); a smaller margin needs more mistakes. With flipped labels no line can be perfect, so the perceptron keeps correcting forever, while the average stays stable.
Why engineers care

The perceptron is a linear classifier you can train in a few lines and run on tiny hardware: one multiply-add per feature. It is still used for fast online classification (for example, branch prediction in CPUs). Its convergence proof is the template for mistake-bound analysis in online learning, and its failure on XOR explains why networks need hidden layers.

📝 Quick check (Level 0)
0/3 answered
  1. 1. The perceptron changes its weights when…
  2. 2. On data that a line can separate, the perceptron…
  3. 3. In the demo with flipped labels, the boundary…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

Training on the AND gate by hand

The data are (0, 0) → −1, (0, 1) → −1, (1, 0) → −1 and (1, 1) → +1. Start from w = (0, 0), b = 0. A score of exactly 0 counts as a mistake.

Point Label Score w·x + b Mistake? New w New b
(0, 0) −1 0 yes (0, 0) −1
(0, 1) −1 −1 no (0, 0) −1
(1, 0) −1 −1 no (0, 0) −1
(1, 1) +1 −1 yes (1, 1) 0

The perceptron keeps cycling. After 18 mistakes over 8 epochs it reaches w = (3, 2), b = −4. The scores are then −4, −2, −1 and +1: all correct, and epoch 9 makes no mistakes.

The same procedure on XOR, (0, 0) → −1, (0, 1) → +1, (1, 0) → +1, (1, 1) → −1, never stops: no single line separates the two classes.

The steps in plain English:

  1. Encode the labels as −1 and +1, and start with w = 0, b = 0.
  2. For each training point, compute the score w·x + b.
  3. If the sign is wrong (or the score is 0), add y·x to w and y to b.
  4. Repeat passes over the data until a full pass has no mistakes, or a maximum number of passes is reached.
  5. Predict the sign of w·x + b.
📝 Quick check (Level 1)
0/3 answered
  1. 1. Update on a mistake with label −1 at x = (2, 1)?
  2. 2. Final AND weights from the table's procedure?
  3. 3. Why does XOR never converge?

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 30 minUndergraduate students and practising engineers.

The algorithm and its variants

Model: ŷ = sign(w·x + b). Fold the bias in by appending a constant 1 to x.

Update on a mistake, yᵢ(w·xᵢ) ≤ 0: w ← w + η yᵢxᵢ. The learning rate η does not matter when w starts at 0: it only rescales w.

As gradient descent. The perceptron is stochastic (sub)gradient descent on the perceptron loss max(0, −y·w·x). Compare the hinge loss max(0, 1 − y·w·x) of the SVM (D5), which also demands a margin.

Variants:

  • Averaged (voted) perceptron (Freund and Schapire, 1999): predict with the average of all weight vectors visited. This greatly reduces variance and gives generalisation bounds. It is the standard choice in practice.
  • Pocket algorithm (Gallant, 1990): keep the weights with the best training accuracy seen so far.
  • Margin perceptron: update when y·w·x is below a margin, not just below 0.
  • Kernel perceptron: w = Σαᵢyᵢφ(xᵢ), so it only needs dot products, giving non-linear boundaries.
  • Multiclass perceptron: one weight vector per class; on a mistake, add x to the true class and subtract it from the predicted class.
from sklearn.linear_model import Perceptron
clf = Perceptron(max_iter=100, tol=None, shuffle=True, random_state=0).fit(X, y)
# Averaged perceptron: SGDClassifier(loss="perceptron", average=True, learning_rate="constant", eta0=1.0)

Use it when you need a very simple, fast online linear classifier, or as a teaching baseline. Prefer logistic regression or a linear SVM for probabilities, margins and noisy data.

🐍 Exercise 1: the perceptron and Novikoff's bound
Implement the perceptron (with optional shuffling and weight averaging), prediction and the geometric margin; check that it learns AND but not XOR, and that on data separable with margin γ it converges within (R/γ)² mistakes.
📝 Quick check (Level 2)
0/3 answered
  1. 1. The perceptron loss is…
  2. 2. The averaged perceptron predicts with…
  3. 3. With w₀ = 0, the learning rate…

Level 3: Advanced

★★★★☆ Advanced⏱ 30 minGraduate level, with full derivations and proofs.

Novikoff’s theorem

Setting. Suppose ‖xᵢ‖ ≤ R, and some unit vector u separates the data with margin γ: yᵢ(u·xᵢ) ≥ γ for all i.

Claim. The perceptron makes at most (R/γ)² mistakes, whatever the order of the points.

Proof. Let w_k be the weights after k mistakes.

  1. Progress: each mistake adds yᵢxᵢ, so u·w_k ≥ u·w_(k−1) + γ, which gives u·w_k ≥ kγ.
  2. Bounded growth: ‖w_k‖² = ‖w_(k−1)‖² + 2yᵢ(w_(k−1)·xᵢ) + ‖xᵢ‖² ≤ ‖w_(k−1)‖² + R². The middle term is ≤ 0 because it was a mistake. So ‖w_k‖² ≤ kR².
  3. Combine: kγ ≤ u·w_k ≤ ‖w_k‖ ≤ √k·R, so k ≤ (R/γ)². ∎

Consequences:

  • The bound does not depend on the dimension or the number of points, only on the geometry. This is the first instance of margin-based learning theory.
  • It is a worst case over orders. On random orders the perceptron usually makes far fewer mistakes: 6–90 against bounds of 71–5927 in exercise 2.
  • Non-separable data: the weights stay bounded but cycle (the perceptron cycling theorem). The last iterate keeps changing, which is why averaging or pocketing is needed.

Minsky and Papert (1969) showed that single-layer perceptrons cannot compute XOR or connectedness. This contributed to the first “AI winter”. Multi-layer networks trained by backpropagation (E0, E6) removed the limitation.

Interview questions.

  • Perceptron or logistic regression? The perceptron stops at any separating line and gives no probabilities. Logistic regression optimises a smooth loss, gives calibrated probabilities, and behaves well with noise.
  • Does the perceptron find the maximum-margin separator? No, just some separator. The SVM finds the maximum-margin one.
📝 Quick check (Level 3)
0/3 answered
  1. 1. Novikoff's mistake bound is…
  2. 2. The bound depends on the dimension…
  3. 3. On non-separable data the perceptron's weights…

Level 4: Expert

★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.

A hazard detector for a lander

History.

  • McCulloch and Pitts (1943) modelled the neuron as a threshold unit.
  • Rosenblatt (1958) built the perceptron and its learning rule; the Mark I Perceptron learned to recognise simple shapes from a 20×20 photocell array.
  • Novikoff (1962) proved convergence.
  • Minsky and Papert’s Perceptrons (1969) exposed its limits.
  • Freund and Schapire (1999) revived it with the voted perceptron.
  • Collins (2002) applied structured perceptrons to NLP tagging.
  • Perceptron branch predictors (Jiménez and Lin, 2001) shipped in commercial CPUs.

Exercise 1. The perceptron learns AND but never XOR. On 2-D data separable with margin 0.106, it converged after 10 mistakes, against Novikoff’s bound of 258, and found a separator with margin 0.103.

Exercise 2: lander hazard detector. Setup: the features are slope and roughness; the true boundary is 0.8·slope + 0.6·roughness = 0.1.

(A) Mistakes before convergence (20 datasets per margin):

Margin γ 0.2 0.1 0.05 0.02
Mean mistakes 6 19 54 88
Novikoff bound (R/γ)² 71 281 1101 5927

The bound always holds but is 10–70 times too pessimistic. Actual mistakes grow far more slowly than 1/γ².

(B) Noisy labels, 20 epochs. The table shows the accuracy on clean test labels.

Flipped labels Last iterate Averaged Pocket
5% 0.868 0.982 0.936
10% 0.826 0.972 0.937
20% 0.688 0.961 0.935
  • The last iterate is unreliable: it is whatever weights the most recent flipped label produced. Its test accuracy swings by 7–15 percentage points from epoch to epoch.
  • Averaging fixes this almost completely: 96–98% accuracy even with 20% noise.
  • The pocket algorithm helps less. Its “best training accuracy” is measured on noisy labels, so it overfits them.
🐍 Exercise 2 (research): mistake bounds and label noise
For a lander hazard detector, measure the perceptron's mistakes before convergence against Novikoff's bound as the margin shrinks; then, with 5–20% flipped labels, compare the clean-test accuracy of the last-iterate, averaged and pocket perceptrons, and the epoch-to-epoch fluctuation of the last iterate.

The perceptron in AI, robotics and aerospace

  • Online classification on small hardware: branch prediction, simple sensor event detection.
  • Neural networks: each neuron is a perceptron with a smooth activation; MLPs stack them (E6).
  • Structured prediction: structured perceptrons for tagging and parsing.
  • Teaching tool: mistake bounds and margins underpin SVMs and online learning theory.

References

  1. F. Rosenblatt (1958). “The perceptron: a probabilistic model for information storage and organization in the brain.” Psychological Review 65(6).
  2. A. B. J. Novikoff (1962). “On convergence proofs on perceptrons.” Symposium on the Mathematical Theory of Automata 12.
  3. M. Minsky, S. Papert (1969). Perceptrons. MIT Press.
  4. Y. Freund, R. E. Schapire (1999). “Large margin classification using the perceptron algorithm.” Machine Learning 37(3).
  5. S. I. Gallant (1990). “Perceptron-based learning algorithms.” IEEE Transactions on Neural Networks 1(2).