Lesson 05
Support Vector Machines
Nine beginner steps: the widest street between pass and fail, support vectors, the margin formula, and a kernel when a line cannot split the classes.
What You Will Learn
Nine steps. Click one to jump.
- 1. What is a Support Vector Machine?
- 2. Types of SVM
- 3. Where is it used?
- 4. How it sits next to other models
- 5. What problem does a careless boundary face?
- 6. How an SVM solves the problem
- 7. Key Takeaways
- 8. Mind Map
- 9. Quiz
1. What is a Support Vector Machine?
Two groups of students. Fail studied fewer hours. Pass studied more. You must draw one boundary and then label a new student.
A careless line squeezes through the gap and almost touches someone. Move one student a little and the line flips. An SVM refuses that skinny cut. It draws the widest street that still keeps fail on one side and pass on the other. The middle of the street is the decision boundary.
The students standing on the two curbs are the support vectors. They alone hold the street in place. A student deep inside a group can shift a bit and the street does not move.
This is the same instinct as regularization. Do not hug the training rows. Leave room for the next person.
2. Types of SVM
| Type | When the picture looks like | What it draws |
|---|---|---|
| Linear | Fail on the left, pass on the right, a gap between them | One straight boundary. In more than two features it is a flat sheet, a hyperplane. |
| Polynomial kernel | The split needs a gentle curve | A curved boundary. Degree 2 is one bend. A huge degree memorizes. |
| RBF kernel | One class sits inside the other, like a ring | A flexible boundary. This is the usual first try when a straight line fails. |
The three pictures above are those three situations. Panel 2 is the linear street. Panel 3 is the ring.
3. Where is it used?
Use an SVM when the job is to name a class, not to predict a mark.
- Pass or fail. Spam or not. Sick or well. Two groups, or a few groups.
- You have a clear gap, or you can allow a kernel to make one.
- The table is medium sized. Thousands of rows is comfortable. Millions of rows gets slow, because the method keeps comparing pairs of rows.
- You care about a new student near the gap, not about threading a line through every old dot.
If you need a number, such as the mark itself, this is the wrong tool. That was the line on the gradient-descent page. SVM answers which side.
4. How it sits next to other models
| Model | What it is trying to do |
|---|---|
| Linear regression | Predict a number. No classes. |
| Logistic regression | Also draws a line between classes, then reports a probability. |
| SVM | Draws the line with the widest street. It does not start from a probability. |
| Neural network | Many stacked cuts. Use it when the groups are tangled and you have a lot of rows. |
A linear SVM and logistic regression often agree on easy tables. They disagree when the gap matters: SVM spends its effort on the street width.
5. What problem does a careless boundary face?
Six students. Only study hours, so the picture is one line of numbers.
| Hours | Result |
|---|---|
| 2 | Fail |
| 3 | Fail |
| 4 | Fail |
| 6 | Pass |
| 7 | Pass |
| 8 | Pass |
A careless rule could say "the cut is at 4.1 hours." Fail at 4 hours is almost standing on the line. A new student who studied 4.2 hours is labelled pass, even though they are nothing like the pass group at 6, 7, and 8.
Another careless rule memorizes: "2 fail, 3 fail, 4 fail, and anyone else pass," including a weird exception if one pass student had a bad day at 5 hours. The next class does not repeat that exception.
The gap you can actually see is from 4 hours (last fail) to 6 hours (first pass). Anything that ignores that empty hour is wasting the only evidence you have.
6. How an SVM solves the problem
Put the middle of the street at 5 hours, halfway from 4 to 6.
| Hours | Side | |
|---|---|---|
| Last fail, a support vector | 4 | left curb |
| Middle of the street | 5 | the boundary |
| First pass, a support vector | 6 | right curb |
| A fail deep inside | 2 | not a support vector |
| A pass deep inside | 8 | not a support vector |
The street runs from 4 to 6. Its width is 2 hours. That width is the margin.
Write the middle line as:
w × hours + b = 0
With the cut at 5, one honest choice is w = 1 and b = −5, because 1 × hours − 5 = 0 when hours = 5.
The two curbs are the same line shifted:
w × hours + b = −1 lands on the fail curb.
w × hours + b = +1 lands on the pass curb.
The street width is 2 / |w|. If you make |w| large, the street gets narrow. SVM wants |w| small, so the street stays wide, while every fail stays on the fail side and every pass stays on the pass side.
Check a new student.
| Hours | w × hours + b = hours − 5 | Label |
|---|---|---|
| 3 | −2 | Fail. Safely past the curb. |
| 4.2 | −0.8 | Fail. Still on the fail side of 5. |
| 7 | +2 | Pass. |
The careless cut at 4.1 would have called 4.2 a pass. The wide street does not.
C is how strict you are about mistakes inside the street. One odd student at 5.2 hours who failed:
- Large C bends the street to keep that one point correct. The street gets narrow. Training looks perfect. A new student near the bend is at risk. This is the same mood as no fine on the regularization page.
- Small C leaves the wide street and allows that one miss. The next class is safer.
Kernel, when a line cannot do it. Suppose pass students studied about 5 hours and fail students studied either 1 hour or 9 hours. Pass is in the middle. No straight cut keeps all fails on one side. The kernel trick does not plot a third axis on paper for you. It lets the SVM score pairs of students as if they had been lifted, and then the wide-street idea runs in that lifted space.
| Kernel | In words | Formula, for two rows x and z |
|---|---|---|
| Linear | Compare them as they are | x · z |
| Polynomial | Allow bends. d is the degree, c is a small constant | (x · z + c) raised to d |
| RBF | Close rows count as similar. γ sets how fast similarity fades | exp( −γ × distance(x, z)² ) |
Start with linear. If the groups wrap around each other, try RBF before a polynomial of degree 20. A huge degree memorizes, the same way a huge weight did.
Scale the columns first. If hours are 2–8 and a second column is income in thousands, distance is dominated by income and the street is meaningless.
7. Key Takeaways
- An SVM separates classes with the widest street, not the skinniest cut.
- Support vectors are the students on the curbs. They define the street. The others do not.
- The middle line is w × hours + b = 0. Street width is 2 / |w|.
- On our table the street sits at 5 hours, from the fail at 4 to the pass at 6.
- Large C hugs training mistakes and can overfit. Small C keeps the street wide.
- A kernel is how you separate groups that are not split by a straight line. RBF is the usual next try.
- Scale the columns. The street is made of distances.
8. Mind Map
Cover the corner boxes and say them yourself. Boundary, curbs, width, lift, strictness.
9. Quiz
The questions below use this street: margin, support vectors, C, and why a kernel exists. The 4-hour fail and the 6-hour pass are the curbs in the lesson.
Check your understanding
Support Vector Machines Quiz
What does the margin in an SVM measure?
1 / 11