The first SVM lecture used a linear score and slack variables for overlapping classes. Its dual formulation depends on inner products between observations. We now use that property to make nonlinear boundaries, then revisit the soft-margin objective as a penalized model.
In the derivations, the class labels are and the decision is based on the sign of . The mixture example keeps its original 0/1 labels in the code; the software handles the two-class encoding.
Learning goals
By the end of this lecture, you should be able to:
explain how a kernel replaces inner products of transformed predictors;
describe how and the radial-kernel scale affect a fitted rule;
tune both parameters using the same cross-validation folds; and
connect slack variables to hinge loss and the penalized SVM objective.
Nonlinear SVM via the kernel trick
A straight boundary may be too restrictive. One approach is to create nonlinear features, such as or , and fit a linear SVM using them. Write this feature map as
The score becomes . It is linear in the new features but can give a nonlinear boundary in the original predictors.
Recall that the dual only needs inner products. If a kernel computes
we can replace the inner products without explicitly forming all the new features. This is the kernel trick. The dual becomes
For a new observation, the fitted score is
Only support vectors contribute because the other values are zero. We still decide by the sign of the score.
For example, consider . Expanding the square gives
This is the inner product of feature vectors containing all ordered second-order products. Computing the original inner product and squaring it avoids explicitly creating features. That is the computational saving in this example. Kernel SVMs can still be expensive with many observations because their fitting problem uses pairwise similarities.
Example: mixture.example data
We return to the mixture data from the KNN lecture. This example lets us compare a straight boundary with a nonlinear one on the same observations. The known class probabilities give a reference Bayes boundary at probability .
First fit a linear SVM with . We use kernlab::ksvm to show another R implementation. Scaling is disabled here so the coefficients and plotted coordinates use the same units.
The linear SVM captures a broad separation, but its straight boundary cannot follow the curved Bayes boundary. The few circled observations in this plot are the support vectors on a margin; other support vectors may lie inside it.
Let’s try the radial kernel,
Nearby observations have a kernel value close to one. The similarity decays with distance, and a larger makes that decay faster. We use and in both languages.
In this dataset, e1071 reports positive decision values for class 0. We negate them so a positive score means class 1, as in Python. This changes the sign convention, not the boundary at zero or the predicted labels.
The radial kernel lets the fitted boundary bend with the mixture structure. It does not reproduce the Bayes boundary exactly, and overlap still makes some class labels difficult to predict. This figure illustrates flexibility; it does not estimate test accuracy.
Other common choices include
Kernel
Formula
Linear
Degree- polynomial
Radial
Sigmoid
For the polynomial kernel, take to be a positive integer and . For the radial kernel, take .
A valid kernel must act like an inner product. In particular, its matrix of pairwise values must be positive semidefinite for every finite collection of inputs. The sigmoid formula does not satisfy this requirement for arbitrary parameter choices; we focus on the linear, polynomial, and radial choices here.
Cross-validation for the radial SVM
The radial kernel introduces a second tuning parameter. We use a grid of four costs and three kernel scales, with five-fold cross-validation. In kernlab and caret, the radial parameter is named sigma; it equals gamma in e1071 and scikit-learn when the predictors use the same scaling.
The supplied mixture fold assignments keep this comparison the same across languages. As before, we standardize within each training fold.
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook. On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
For these folds, and give the largest mean cross-validation accuracy, . This is a different fit from the unscaled illustration with and . The result is best among the candidates we tried; it does not establish that this grid contains the best possible settings.
SVM as a penalized model
Recall the soft-margin problem from the first lecture. It minimizes , subject to and . For fixed , the smallest allowed slack is , where . Substituting this value and dividing the objective by gives the equivalent problem
This has the familiar form loss plus penalty. The first term is the hinge loss: it is zero for an observation on or beyond its margin and increases as that observation moves inside the margin. The second term discourages a large coefficient norm. A smaller means a larger , so the penalty receives more weight.
Here, denotes the penalty parameter when hinge losses are summed. In the ridge and lasso lectures, we averaged the loss, so the numerical scale of the penalty differs. Compare the full objective before comparing numerical penalty values.
Compare hinge loss with three other losses. In the figure, is the signed score: positive values indicate a correct decision, and values of at least one satisfy the margin. Zero-one loss counts mistakes, while hinge loss also responds to correct decisions inside the margin. Logistic and modified Huber losses show smoother alternatives. The logistic curve, , uses the same natural logarithm as the log loss in Week 6.
For a nonlinear SVM, use the same objective with the feature map in place of the original predictors. The penalty measures the size of the coefficients in that feature space. We do not need to construct those features explicitly: the kernel representation of above uses only similarities to training observations. Thus the loss-and-penalty view and the kernel trick describe the same fitted classifier from two perspectives.
Check your understanding
What calculation does the kernel trick avoid, and what pairwise calculations remain?
How does increasing change the similarity between two distinct observations?
Why should and be chosen together using validation data?
Why can two positive signed scores give the same class decision but different hinge losses?
Key ideas
A kernel replaces inner products of mapped features and can produce a curved boundary in the original predictors.
The linear and radial fits on the mixture data show what changes when the boundary can bend. Use validation to compare predictive performance.
The radial SVM has both a cost and a kernel scale to tune.
The soft-margin objective is hinge loss plus a coefficient penalty, with in the form used here.
An SVM score can be used for an ROC curve once its direction is checked, but it is not automatically a probability.
Additional reading
For optional details on the dual derivation and the loss-and-penalty view, see the corresponding sections of the SMLR support vector machine notes.