Sub topic 4
Soft margins and slack variables
This chapter assumes you know the hard-margin SVM from the previous chapter, the equation of a hyperplane, and the difference between training error and test error. It covers how the soft-margin SVM allows some points to break the margin, and how slack variables measure the violations.
Why hard margins fail
The hard-margin SVM requires every point to satisfy yi(w·xi + b) ≥ 1. This has two problems:
- If the classes overlap, no hyperplane satisfies all constraints, so the problem has no solution.
- A single outlier can force the boundary to tilt sharply, even when the classes are almost separable.
The soft-margin SVM fixes both by letting some constraints be violated, at a price.
Slack variables
Each point gets a slack variable ξ_i ≥ 0. The constraints become:
yi(w·xi + b) ≥ 1 − ξ_i
The optimization problem is:
minimize 0.5·||w||² + C·Σ ξi, subject to the constraint above and ξi ≥ 0.
The value of ξ_i tells you where point i sits:
| Slack value | Meaning |
|---|---|
| ξ_i = 0 | Correct side, outside or on the margin |
| 0 < ξ_i ≤ 1 | Correct side, but inside the margin |
| ξ_i > 1 | Misclassified |
The first term in the objective keeps the margin wide. The second term penalizes total slack. The constant C sets the exchange rate between the two, and the next chapter covers it.
Computing slack in code
At the optimum, each slack equals max(0, 1 − yi(w·xi + b)). You can compute it from a fitted model. Labels must be in {−1, +1} for this formula.
import numpy as np
from sklearn.datasets import make_blobs
from sklearn.svm import SVC
X, y = make_blobs(n_samples=100, centers=2, cluster_std=2.5, random_state=0)
y_pm = np.where(y == 1, 1, -1)
clf = SVC(kernel="linear", C=1.0).fit(X, y)
w, b = clf.coef_[0], clf.intercept_[0]
margins = y_pm * (X @ w + b)
slack = np.maximum(0.0, 1.0 - margins)
print("points with slack > 0:", int((slack > 0).sum()))
print("misclassified (slack > 1):", int((slack > 1).sum()))
print("support vectors:", int(clf.n_support_.sum()))The count of points with positive slack is never larger than the count of support vectors. Every point with ξ_i > 0 is a support vector. Some support vectors sit exactly on the margin with zero slack.
Common errors
- Using labels {0, 1} in the margin formula. The formula y(w·x + b) needs labels in {−1, +1}. With {0, 1}, every class-0 point gets margin 0.
- Treating slack as a model output. Slack is an optimization variable. You derive it from the fitted w and b, as shown above.
- Assuming a soft margin means a bad fit. Allowing some violations usually improves test accuracy on noisy data.
Exercise
A point has y = +1 and w·x + b = −0.4. What is its slack, and how is the point classified?
Show answer
The margin is y(w·x + b) = −0.4. Slack is max(0, 1 − (−0.4)) = 1.4. Because the slack exceeds 1, the point is misclassified.
Exercise
Why can a hard-margin SVM fail on a dataset where one point of class A lies deep inside class B's region?
Show answer
No hyperplane can put every point on its correct side with margin at least 1, so the constraints cannot all hold and the problem has no solution. The soft-margin SVM gives that point large slack and still returns a boundary.