Support vector machines
Last updated
Many straight lines can split two classes. A support vector machine picks the one with the widest street around it, the most room for error either side, and that street is held in place by just a few points, the support vectors. Then a trick lets the same method draw curves: measure similarity in a way that secretly lifts the data into more dimensions, where a flat cut is enough.
By the end of this lesson you can
- Explain the widest margin and the support vectors that fix it
- Choose C for a soft margin, and connect it to the hinge loss
- Explain the kernel trick, and tune the RBF kernel’s γ
- Fit SVC and SVR in a pipeline with scaling, and say when to use them
The widest street
Logistic regression (lesson 3.9) finds a line by making the training data as likely as possible. A support vector machine asks a different question: of all the lines that split the classes, which has the most room to spare?
Picture the line as the middle of a street. Widen the street, equally both sides, until it touches a point of either class. The best line is the one whose street is widest. With weights and bias , the line is , and its two edges are where . The street's width works out as
so the widest street is the smallest that keeps every point on its own side of the street:
with the labels written as and . A wide street is a cautious boundary: new points can land a little off from the training points and still be classified the same way.
Support vectors
Two clusters of 30 points, and a linear SVM with no points allowed in the street:
Of 60 points, only 2 decide the answer. They're the support vectors: the points on the edges of the street. The street is 0.71 wide. Every other point could move anywhere on its own side, and the line wouldn't change. Try it: drag the points.
The street is 0.73 wide, and 3 support vectors (ringed) hold it in place. With C this large, no point may sit inside the street: a hard margin.
Drag a point that isn't ringed: nothing happens. Drag a ringed one towards the other class: the street narrows and turns. Drag a point right across, and with the hardest setting of C, the street has to squeeze until no point is inside it. That's fragile. One mislabelled point can wreck the boundary.
A soft margin, and C
Real classes overlap, and then no street is empty. A soft margin (Cortes and Vapnik, 1995) lets points inside the street, or even on the wrong side, at a price. The setting C is that price:
- Large C: points inside the street are expensive. A narrow street that tries to get every training point right. Lower bias, higher variance.
- Small C: they're cheap. A wide street that ignores a few awkward points. Higher bias, lower variance.
Move the C slider in the figure above, then compare on overlapping data:
The columns are C, the number of support vectors, the street's width, and the test accuracy. With C = 0.01 the street is 3.55 wide and 117 of the 150 training points are support vectors: everything in or near the street counts. With C = 100, it's 1.68 wide with 73. Here the test score is the same 85.3% for all three, because the classes overlap so much that the best line is the best line; on other data it isn't, and C is chosen by cross-validation (lesson 4.13).
The hinge loss
The soft-margin SVM is also a loss to minimise, like every model in Stage 3:
The second part is the hinge loss. A point safely beyond its edge of the street () costs nothing at all. Inside the street, or on the wrong side, the cost grows in a straight line. Compare logistic regression's cross-entropy (lesson 3.10), which never quite reaches 0, so every point, however safe, pulls on the line a little. With the hinge, only the points in or near the street pull at all, which is why they're the only ones that matter.
The first part, , is a ridge penalty (lesson 3.8), and C works like : a large C means a weak penalty.
The kernel trick
The circles data, a ring inside a ring, can't be split by any line. Lesson 4.2 fixed it with a feature, . Seen in three dimensions:
In two dimensions, the inner ring (pink circles) sits inside the outer one (squares): no straight line can split them. Raise the lift.
Lifted, a flat plane separates the rings, and seen from above that plane's cut is a circle. An SVM can do this lifting without ever computing the new features. Its answer depends on the data only through dot products between pairs of points, , and a kernel is a function that gives the dot product the points would have after some lift, straight from the originals. The most used is the RBF (radial basis function) kernel:
1 for identical points, falling towards 0 as they move apart. It corresponds to a lift into infinitely many dimensions, and costs no more than a distance.
A linear SVM on the plain circles: 49%. Linear on the lifted data: 99.7%. The RBF kernel on the plain data, with no lifting by hand: also 99.7%.
Wrong way: an island around every point
says how far each point's influence reaches. Small : far, so the boundary is smooth. Large : only very close, so the boundary can bend around single points. On the moons:
With , 91% on test. With , 100% on training, with all 200 points as support vectors, and 57.5% on test, barely better than a coin:
Each training point has its own tiny island of its class, and new points, which fall between the islands, get the background answer. It's lesson 2.6's overfitting, in its purest form. C and are tuned together, on a log scale (lesson 4.13).
Scaling, and SVMs for numbers
The RBF kernel is built on distances, so the feature with the biggest numbers dominates (lesson 4.1). On the penguins, body mass in grams swamps bill depth in millimetres:
72.8% unscaled, 98.2% scaled. Always put an SVM in a pipeline after a scaler.
SVR, support vector regression, turns the street around: it fits a tube of half-width around the data, ignores errors smaller than , and pays for the rest, linearly. On the noisy wave from lesson 4.6:
51 of the 80 points are outside the tube and become support vectors, and the fit's RMSE is 0.185.
When to use it
Good at
- Small and medium data, up to tens of thousands of rows
- Curved boundaries, with the RBF kernel
- Many features compared with rows (a linear SVM)
Bad at
- Big data: training time grows faster than the number of rows
- Probabilities: it gives a score, and probability=True adds a slow, extra fit
- Reading the model, with a kernel
- Unscaled features
Needs from you
- Scaled features, always
- C and γ, tuned together on a log scale
In scikit-learn
- Classification:
SVC,LinearSVC(for big data) - Regression:
SVR,LinearSVR
Check what you learned
Sources
Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.
- Cortes and Vapnik, Support-vector networks, Machine Learning 20, 1995: the soft-margin support vector machine
- scikit-learn: Support vector machines: SVC, SVR, kernels, C and gamma, and the advice to scale
- scikit-learn: RBF SVM parameters: how C and gamma shape the boundary