Support Vector Machine

Support Vector Machine

Find the widest street that separates two classes.

Drag any student, or change C. The border keeps the widest street it can between the two groups.

  • passed
  • failed: drag any student
  • support vector
  • margin

f(x)=w⋅x+b\htmlClass{sym-line}{f}(x) = w \cdot x + b

f(x) = 0.52 × studied + 0.09 × slept − 3.04

margin = 2 ÷ ‖w‖ = 2 ÷ 0.52 = 3.82 h

½‖w‖² + C Σξ = 0.14 + 1.00 × 3.80 = 3.93

Student 19 (failed): y·f(x) = −1 × 0.62 = −0.62, on the wrong side of the border; ξ = 1.62

Support vectors 6 of 24
2 on the wrong side

Students and a test: 24 students, and where each one sits against the border.
13.23.2fail1.100.00–
28.98.3pass2.310.00–
32.12.7fail1.710.00–
49.24.9pass2.150.00–
56.23.2fail−0.451.45yes
62.86.5fail1.000.00yes
72.95.7fail1.020.00–
80.87.3fail1.960.00–
96.77.4pass1.090.00–
108.35.1pass1.710.00–
110.76.2fail2.110.00–
128.43.9pass1.650.00–
132.39.2fail1.010.00–
147.76.8pass1.550.00–
151.35.6fail1.860.00–
166.99.5pass1.390.00–
177.15.5pass1.130.00–
187.95.8pass1.570.00–
196.15.6fail−0.621.62yes
202.05.6fail1.490.00–
215.55.2pass0.270.73yes
227.33.0pass1.000.00yes
236.29.2pass1.000.00yes
247.87.6pass1.680.00–

Try it in Python

The same 24 students (where you dragged them), C and border as the Lab, in Python. scikit-learn uses the Lab's solver; Plain Python finds the same border with a simpler one, two weights at a time.

What you are looking at

Each shape is a student. Across the graph is how many hours they studied before a test; up the graph is how many hours they slept. Circles passed and triangles failed. Studying and sleep both help, so the border between them runs on a slant, and a few students break the rule.

The solid line is the border the model draws. The dashed lines on either side are the edges of its margin, like the kerbs of a street. Students with a ring are the support vectors. The numbers are made up, and Refresh Data shows another class.

What each symbol means

x
A student: hours studied and hours slept.
y
+1 for passed, −1 for failed.
f(x)
The model's score. Above 0: pass. Below 0: fail. Exactly ±1 on the dashed lines.
w, b
For a straight border, the weights of the two features and the intercept.
ξ
Slack: how far a student is past their own dashed line. 0 for students safely on their side.
C
How much each bit of slack costs.
γ
For a curved border: how far each student's pull reaches.

How the model finds its border

Many straight lines could separate the two groups. A support vector machine picks the one with the widest street: the line that stays as far as possible from the nearest students on both sides.

f(x)=w⋅x+b\htmlClass{sym-line}{f}(x) = w \cdot x + b

The street's edges are where f(x) = +1 and −1, so its width is 2 ÷ ‖w‖. A wide street means a small w.

margin=2∥w∥\text{margin} = \frac{2}{\lVert w \rVert}

Only the students on or inside the street decide where it goes: these are the support vectors. Drag a student far from the border and nothing moves. Drag a support vector and the whole street shifts.

C: a wide street, or few mistakes?

When the groups overlap, no street is empty. The model lets some students stand inside it, or even on the wrong side, and charges C for each unit of slack. It then makes the total as small as it can:

min⁡w, b  12∥w∥2+C∑iξi,ξi=max⁡(0,  1−yif(xi))\min_{w,\,b}\; \tfrac{1}{2}\lVert w \rVert^2 + \htmlClass{sym-a}{C} \sum_i \htmlClass{sym-error}{\xi_i}, \quad \htmlClass{sym-error}{\xi_i} = \max\big(0,\; 1 - y_i \htmlClass{sym-line}{f}(x_i)\big)

A small C makes slack cheap: the street gets wide and many students lean on it, so the border depends on many of them and stays calm. Alarge C makes slack expensive: the street narrows to squeeze students out of it, and a single odd student can swing the border.

Curved borders and γ

Turn on Curved and the border may bend. Instead of a straight line, each support vector pulls the score towards its own side, strongly when it is close and weakly when it is far:

f(x)=∑iαi yi e−γ∥xi−x∥2+b\htmlClass{sym-line}{f}(x) = \sum_{i} \alpha_i\, y_i\, e^{-\htmlClass{sym-b}{\gamma} \lVert x_i - x \rVert^2} + b

γ sets how far that pull reaches. A small γ gives each student a wide reach, so the border stays smooth and close to straight. A large γ gives each student a tiny reach, so the border wraps around single students and makes small islands. That fits these students but would guess badly for new ones.

The model in short

  1. Draw the border with the widest street between the groups.
  2. Allow slack for students inside the street, at a price C.
  3. Keep only the support vectors: they alone hold the street in place.
  4. Bend the border with a curved kernel when a straight one cannot fit, and use γ to keep it from bending too much.