The first artificial neurons · lesson 3 of 7
Parts in this lesson
Anp.abs(X @ w_star).min()the margin: the closest any point comes to the line
Bnp.linalg.norm(X, axis=1).max()R: the farthest any point is from the origin
C(R / gamma) ** 2Novikoff's bound on the number of mistakes

Why the perceptron works

Last updated

Lesson 5.2's perceptron stopped after a few updates on one pair of species and never stopped on another. Is that luck? In 1962 Albert Novikoff proved it isn't: if a line separates the data with some room to spare, the perceptron will find a separating line after a limited number of mistakes, and the limit depends only on how much room there is. This lesson states the theorem, proves it in four lines, and counts mistakes against it.

By the end of this lesson you can

  • Define linearly separable data and its margin
  • State Novikoff's bound, and say what it needs and what it doesn't depend on
  • Follow the four-line proof
  • Compare the bound with real mistake counts as the margin shrinks

Linearly separable data

Data with two classes is linearly separable if some line (in more dimensions, some hyperplane) has every example of one class on one side and every example of the other class on the other. Adelie and Gentoo penguins, by bill depth and flipper length, are; Adelie and Chinstrap, by bill length and depth, aren't (lesson 5.2). XOR's four points aren't either (lesson 5.1).

To keep the maths short, this lesson leaves out the bias, so every line passes through the origin. That costs nothing: add a feature that's always 1, and its weight does the bias's job. A line through the origin is then described by a vector w at right angles to it, and w⊤x says which side x is on.

The margin

Suppose a unit vector w⋆ (length 1) separates the data. For a unit vector, |w⋆⊤x| is the distance from x to the line. The margin γ (gamma) is the smallest of those distances: how close the nearest example comes to the line. In symbols, every example satisfies

yiw⋆⊤xi≥γ

which says it's on its correct side, at least γ away. A big margin means an empty street between the classes, like the SVM's in lesson 4.7; a small margin means some examples almost touch the line.

The other number is R, the distance from the origin to the farthest example: ‖xi‖≤R for every i. It measures how spread out the data is.

novikoff.ipynb

200 points in a disc of radius 1, kept only if they're at least 0.2 from the true line, with half of them just outside that distance. R=0.998 and γ=0.2.

Novikoff's bound

Novikoff's theorem (1962). If every example has ‖xi‖≤R, and some unit vector w⋆ separates the data with margin γ>0, then the perceptron, started at w=0 with any learning rate and visiting the examples in any order, makes at most

(Rγ)2

mistakes. After that many, it can't make another, so every pass is clean and it stops.

For the data above, the bound is (0.998/0.2)2≈25. The perceptron made 2.

Look at what the bound doesn't depend on: the number of examples, the number of features, the order of the examples, or the learning rate. A million examples in a thousand dimensions are no harder than 200 in two, if the ratio R/γ is the same. Only the shape of the data matters: how wide the gap is, compared with how spread out the points are.

And what it needs: a margin greater than 0, which means linearly separable data. On Adelie against Chinstrap there's no w⋆ at all, so the theorem says nothing, and lesson 5.2 found the mistakes never stop.

Why it's true, in four lines

This step is optional. The idea: every mistake makes w point more along w⋆, quickly, but makes w longer only slowly. Since a vector can't point along w⋆ by more than its own length, there can only be so many mistakes.

Let wk be the weights after k mistakes, with w0=0 and η=1. The k-th mistake is on some example (xi,yi), so yiwk−1⊤xi≤0, and the rule sets wk=wk−1+yixi.

  1. It turns towards w⋆. w⋆⊤wk=w⋆⊤wk−1+yiw⋆⊤xi≥w⋆⊤wk−1+γ. Each mistake adds at least γ, so w⋆⊤wk≥kγ.
  2. It grows slowly. ‖wk‖2=‖wk−1‖2+2yiwk−1⊤xi+‖xi‖2≤‖wk−1‖2+R2, because the middle term is ≤0 on a mistake. So ‖wk‖2≤kR2.
  3. Put them together. For a unit vector, w⋆⊤wk≤‖wk‖ (lesson 1.3: a dot product is at most the product of the lengths). So kγ≤‖wk‖≤kR.
  4. Solve for k. Divide by γk: k≤R/γ, so k≤(R/γ)2.

With a learning rate η, every wk is just η times as big, and the same four lines go through. That's why the bound doesn't care about η.

Counting mistakes

The bound is a guarantee for the worst case. How close does a real run come? Shrink the margin and count:

Mistake countermargin γ = 0.2
mistakes made3bound (R/γ)²251101001,00010,000log scale

The shaded band is the margin: no point is closer than 0.2 to the true boundary. R = 0.996. The perceptron made 3 mistakes before a clean pass; the theorem promises at most 25. Make the margin smaller and watch both numbers.

The same in NumPy, for five margins:

novikoff.ipynb

As the margin shrinks from 0.4 to 0.02, the mistakes grow from 1 to 25, and the bound from 6 to 2,485. Two things to see:

  • The bound holds, every time, as the proof says it must.
  • It's very loose. It's the worst case over every possible order of examples, and a shuffled order is nowhere near the worst. Real counts are usually far smaller. The bound's real value is what it says about why: a wide margin makes learning easy, and a thin one makes it hard.

That idea outlived the perceptron. The SVM (lesson 4.7) chooses the widest margin on purpose, and margins are still one of the main ways of explaining why a classifier does well on new data (Freund and Schapire, 1999).

Check what you learned

Data has R = 2 and margin γ = 0.1. At most how many mistakes can the perceptron make?
You double every feature value in a separable data set. What happens to the bound (R/γ)²?
Why doesn’t Novikoff’s theorem promise that the perceptron stops on Adelie against Chinstrap bills?

Sources

Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.

All sources for Simple Machine Learning