A linear kernel with a huge \(C\) gives the maximal margin classifier.
Figure 56.1: Left, three of the infinitely many lines that separate the two classes. Right, the maximal margin classifier, with the margin (dashed) and the three support vectors (circled).
Kernels
SVC uses the radial kernel by default, \(K(x_i, x) = \exp\big(-\gamma \|x_i - x\|^2\big)\).
The decision boundary, with the support vectors marked. Switch to the rings and to the RBF kernel to see a boundary that no hyperplane could produce.
The default SVC() is a radial kernel with \(C = 1\). On three blobs, two of them in the same class, it draws the boundary one would draw by hand.
Figure 56.2: The classifier of SVC() on three blobs, two of them in the same class.
In practice
The kernel uses distances, so the predictors should be standardized. \(C\) and \(\gamma\) are tuned together by cross-validation, here on two noisy rings.
SVC fits one classifier for every pair of classes, each on the points of its two classes only, and predicts the class that wins most pairs.
fitting the three pairwise classifiers and drawing them
from itertools import combinationsX, y = make_blobs(300, centers=[(0, 0), (3, 0), (1.5, 2.6)], cluster_std=0.9, random_state=2)g1, g2 = np.meshgrid(np.linspace(-3, 6, 200), np.linspace(-3, 5.5, 200))grid = np.column_stack([g1.ravel(), g2.ravel()])votes = np.zeros((len(grid), 3))fig, axes = plt.subplots(1, 4, figsize=(12, 3.2))for ax, (a, b) inzip(axes, combinations(range(3), 2)): keep = (y == a) | (y == b) pair = SVC(gamma=0.5).fit(X[keep], y[keep]) p = pair.predict(grid) votes[np.arange(len(grid)), p] +=1 ax.contourf(g1, g2, p.reshape(g1.shape), levels=[-0.5, 0.5, 1.5, 2.5], colors=["C0", "C1", "C2"], alpha=0.2) ax.scatter(X[keep, 0], X[keep, 1], c=[f"C{c}"for c in y[keep]], s=8) ax.set_title(f"class {a} against class {b}")vote = votes.argmax(axis=1)axes[3].contourf(g1, g2, vote.reshape(g1.shape), levels=[-0.5, 0.5, 1.5, 2.5], colors=["C0", "C1", "C2"], alpha=0.2)axes[3].scatter(X[:, 0], X[:, 1], c=[f"C{c}"for c in y], s=8)axes[3].set_title("majority vote")for ax in axes: ax.set(xticks=[], yticks=[], aspect="equal")plt.show()
Figure 56.3: Three classes. The first three panels show the classifier of each pair of classes, fitted on the points of these two classes only. The last one shows the majority vote of the three.
SVC does the same on its own:
svm = SVC(gamma=0.5).fit(X, y)print("same prediction as the vote:", np.mean(svm.predict(grid) == vote))
1
A fixed \(\gamma\), as for the pairs above. The default gamma="scale" computes \(\gamma\) from the spread of the points it is fitted on, which differs between a pair of classes and all three.
same prediction as the vote: 1.0
Handwritten digits
On the MNIST digits, we compare a support vector machine with logistic regression and a random forest, fitted on the first 5000 training images and tested on the 10000 test images. All pixels are on the same scale, from 0 to 1, so we do not standardize them.
logistic regression test accuracy 0.895, prediction of 10000 images 0.0 s
random forest test accuracy 0.942, prediction of 10000 images 0.7 s
SVC() test accuracy 0.951, prediction of 10000 images 11.3 s
support vectors: 2379
The support vector machine is the most accurate, but it is slow at prediction: every test image is compared with each of the 2379 support vectors, almost half of the training images. Tuning \(C\) and \(\gamma\) helps a little more.