Traditional models · lesson 7 of 16
Parts in this lesson
ASVC(kernel='linear', C=1000)the widest street, with no points inside
Bsvm.support_vectors_the points that hold the street
CSVC(kernel='rbf', gamma=1)curved boundaries, through a kernel
Dmake_pipeline(StandardScaler(), SVC())4.1: scaling first, always

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 w and bias b, the line is w⋅x+b=0, and its two edges are where w⋅x+b=±1. The street's width works out as

width=2‖w‖

so the widest street is the smallest ‖w‖ that keeps every point on its own side of the street:

minw,b⁡12‖w‖2subject toyi(w⋅xi+b)≥1 for every i

with the labels yi written as +1 and −1. 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:

svm.ipynb

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.

Margin streetdrag any point; C = 1000

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:

svm.ipynb

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:

minw,b⁡12‖w‖2+C∑imax⁡(0,1−yi(w⋅xi+b))

The second part is the hinge loss. A point safely beyond its edge of the street (yif(xi)≥1) 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, 12‖w‖2, is a ridge penalty (lesson 3.8), and C works like 1/λ: 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, x2+y2. Seen in three dimensions:

Kernel liftcircles, lifted by z = x² + y²
flat: x and y only

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, xi⋅xj, 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:

K(x,x′)=exp⁡(−γ‖x−x′‖2)

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.

svm.ipynb

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:

svm.ipynb

With γ=1, 91% on test. With γ=1000, 100% on training, with all 200 points as support vectors, and 57.5% on test, barely better than a coin:

svm.ipynb

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:

svm.ipynb

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:

svm.ipynb

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

When to use a support vector machine

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

You remove a training point that isn’t a support vector and refit. What happens to the boundary?
An RBF SVM scores 100% on training data and 60% on test data. Which change is most likely to help?
What does a kernel let an SVM do?

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.

All sources for Simple Machine Learning