Sub topic 10
Choosing a kernel: linear, polynomial, RBF
This chapter assumes you know the dual SVM and the kernel trick from the previous chapter. It also assumes you can read basic scikit-learn and NumPy code. The kernel trick lets an SVM use a function K(x, z) in place of an explicit dot product in a high-dimensional feature space. Here you will choose which function to use.
The three common kernels
| Kernel | Formula | Main parameters | Boundary shape |
|---|---|---|---|
| Linear | `K(x, z) = x·z` | `C` | Flat hyperplane |
| Polynomial | `K(x, z) = (gamma·x·z + r)^d` | `C`, `degree`, `gamma`, `coef0` | Smooth curves up to degree `d` |
| RBF | `K(x, z) = exp(-gamma·‖x − z‖²)` | `C`, `gamma` | Flexible, local bumps |
In scikit-learn, r is coef0 and d is degree.
Linear kernel
The linear kernel keeps the original feature space. Use it when you have many features relative to samples, such as text with thousands of sparse word counts. In that setting the classes are often already close to linearly separable. It trains fast, has only C to tune, and gives a weight vector w you can inspect.
Polynomial kernel
The polynomial kernel implicitly adds all feature products up to degree d. A degree-2 kernel can learn an ellipse in two dimensions. It suits problems where you expect interactions between features. Large degrees make the kernel values grow or shrink quickly, which causes numerical trouble and overfitting. Most practical uses stay at degree 2 or 3.
RBF kernel
The RBF kernel is a similarity that equals 1 when two points coincide and decays toward 0 with distance. It corresponds to an infinite-dimensional feature space, so it can fit very flexible boundaries. It is the usual default when you have no prior knowledge of the data's shape. Its gamma parameter is covered in the next chapter.
Comparing kernels in code
The snippet below fits all three kernels on data that a straight line cannot separate.
from sklearn.datasets import make_moons
from sklearn.model_selection import cross_val_score
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVC
X, y = make_moons(n_samples=400, noise=0.25, random_state=0)
for kernel in ["linear", "poly", "rbf"]:
model = make_pipeline(StandardScaler(), SVC(kernel=kernel, C=1.0))
scores = cross_val_score(model, X, y, cv=5)
print(f"{kernel:7s} mean accuracy: {scores.mean():.3f}")Expect the linear kernel to score lowest, because the two moons curve around each other. The RBF kernel usually scores highest here.
Common errors
- Choosing a high-degree polynomial kernel when RBF would do.
- Judging a kernel with default parameters only, then dropping it. Each kernel needs its own tuning of
Cand its other parameters. - Skipping feature scaling.
Exercise
You have 500 samples with 20,000 sparse text features. Which kernel do you try first, and why?
Show answer
The linear kernel. With so many features relative to samples, the data is often already close to linearly separable. The linear kernel is fast and has only C to tune. Move to RBF only if cross-validation shows the linear model underfits.
Exercise
In the comparison code, why is StandardScaler placed inside make_pipeline instead of applied to X beforehand?
Show answer
Inside the pipeline, each cross-validation fold fits the scaler only on its training part. Scaling beforehand would let information from the validation folds leak into the scaler's mean and variance.