The perceptron finds any line that separates two classes, and there are infinitely many such lines. The support vector machine (SVM) chooses the best one: the line that stays as far as possible from both classes.
The Maximum-Margin Idea
Suppose the two classes can be separated by a line (a hyperplane in more dimensions), w·x + b = 0. Two parallel lines, w·x + b = +1 and w·x + b = −1, pass through the closest points of each class. The distance between them is the margin, equal to 2 / ‖w‖. The SVM looks for the hyperplane with the largest margin, because a wide margin means the classifier tolerates small changes in the data and generalises better.
Support Vectors
The training points that lie exactly on the margin lines are the support vectors. They alone determine the hyperplane: removing any other point leaves the solution unchanged, while moving a support vector changes it. This makes the SVM compact and robust.
Hard Margin and Soft Margin
A hard-margin SVM requires every point to be classified correctly and works only if the data are perfectly separable. Real data contain noise, so a soft-margin SVM allows some points to violate the margin, at a cost controlled by the parameter C. A large C punishes errors heavily and gives a narrow margin that may overfit; a small C gives a wider margin that tolerates more errors. For data that are not linearly separable, the kernel trick maps the data into a higher-dimensional space, though that is beyond this unit.
Definition: Support vector machine. A classifier that finds the separating hyperplane with the maximum margin between the two classes; the points closest to the hyperplane are the support vectors.
Table 4.1: Hard margin and soft margin
| Aspect | Hard margin | Soft margin |
|---|---|---|
| Errors allowed | None | Some, controlled by C |
| Data required | Perfectly separable | Noisy or overlapping |
| Large C | Not applicable | Narrow margin, may overfit |
| Small C | Not applicable | Wide margin, more tolerance |
Example 4.1: A margin in one dimension
Problem: Class 0 points are x = 0 and 1; class 1 points are x = 3 and 4. Find the maximum-margin boundary, the support vectors and the margin.
Solution:
- The closest points of the two classes are x = 1 and x = 3.
- The best boundary lies midway at x = 2. Writing it as f(x) = w x + b with f(1) = −1 and f(3) = +1 gives w = 1 and b = −2.
- The support vectors are x = 1 and x = 3; the points 0 and 4 do not affect the boundary.
- The margin is 2 / ‖w‖ = 2 / 1 = 2.
Result: Boundary x = 2, support vectors 1 and 3, margin 2.
from sklearn.svm import SVC
X = [[0], [1], [3], [4]]
y = [0, 0, 1, 1]
m = SVC(kernel="linear", C=1e6).fit(X, y)
print("w =", round(float(m.coef_[0][0]), 2),
" b =", round(float(m.intercept_[0]), 2))
print("support vectors:", m.support_vectors_.ravel().tolist())
print("margin =", round(2 / abs(float(m.coef_[0][0])), 2))
print("predict 1.9 and 2.1:", m.predict([[1.9], [2.1]]).tolist())
Output
w = 1.0 b = -2.0
support vectors: [1.0, 3.0]
margin = 2.0
predict 1.9 and 2.1: [0, 1]
Common mistakes
- Thinking all training points influence the SVM boundary; only support vectors do.
- Applying a hard-margin SVM to noisy, overlapping data.
- Confusing the margin (2 / ‖w‖) with the weight vector itself.
- Setting C extremely large and overfitting the noise.
Key takeaways
- SVM picks the hyperplane with the maximum margin.
- Margin = 2 / ‖w‖; support vectors lie on the margin lines.
- Soft margin uses C to trade margin width against errors.
- Only support vectors determine the solution.
Practice questions
- What is the margin of an SVM?
- What are support vectors?
- What does a large C do?
Answers to the practice questions
- The distance between the two parallel lines through the closest points of each class, 2 / ‖w‖.
- The training points lying on the margin that define the hyperplane.
- It punishes errors heavily, giving a narrow margin that may overfit.
Sources
- Official BCA syllabus Introduction to Machine Learning, course code 26BCA404DSE21, Unit IV
