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) = 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
| 1 | 3.2 | 3.2 | fail | 1.10 | 0.00 | – |
| 2 | 8.9 | 8.3 | pass | 2.31 | 0.00 | – |
| 3 | 2.1 | 2.7 | fail | 1.71 | 0.00 | – |
| 4 | 9.2 | 4.9 | pass | 2.15 | 0.00 | – |
| 5 | 6.2 | 3.2 | fail | −0.45 | 1.45 | yes |
| 6 | 2.8 | 6.5 | fail | 1.00 | 0.00 | yes |
| 7 | 2.9 | 5.7 | fail | 1.02 | 0.00 | – |
| 8 | 0.8 | 7.3 | fail | 1.96 | 0.00 | – |
| 9 | 6.7 | 7.4 | pass | 1.09 | 0.00 | – |
| 10 | 8.3 | 5.1 | pass | 1.71 | 0.00 | – |
| 11 | 0.7 | 6.2 | fail | 2.11 | 0.00 | – |
| 12 | 8.4 | 3.9 | pass | 1.65 | 0.00 | – |
| 13 | 2.3 | 9.2 | fail | 1.01 | 0.00 | – |
| 14 | 7.7 | 6.8 | pass | 1.55 | 0.00 | – |
| 15 | 1.3 | 5.6 | fail | 1.86 | 0.00 | – |
| 16 | 6.9 | 9.5 | pass | 1.39 | 0.00 | – |
| 17 | 7.1 | 5.5 | pass | 1.13 | 0.00 | – |
| 18 | 7.9 | 5.8 | pass | 1.57 | 0.00 | – |
| 19 | 6.1 | 5.6 | fail | −0.62 | 1.62 | yes |
| 20 | 2.0 | 5.6 | fail | 1.49 | 0.00 | – |
| 21 | 5.5 | 5.2 | pass | 0.27 | 0.73 | yes |
| 22 | 7.3 | 3.0 | pass | 1.00 | 0.00 | yes |
| 23 | 6.2 | 9.2 | pass | 1.00 | 0.00 | yes |
| 24 | 7.8 | 7.6 | pass | 1.68 | 0.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.
The street's edges are where f(x) = +1 and −1, so its width is 2 ÷ ‖w‖. A wide street means a small w.
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:
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:
γ 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
- Draw the border with the widest street between the groups.
- Allow slack for students inside the street, at a price C.
- Keep only the support vectors: they alone hold the street in place.
- Bend the border with a curved kernel when a straight one cannot fit, and use γ to keep it from bending too much.