Support Vector Machines and Kernels

Maximum-margin classifiers and the kernel trick for nonlinear boundaries on small and medium datasets.

Intermediate · about 5 hours · difficulty 3/5

Learn first: Logistic Regression and Linear Classifiers

SVMs find the boundary that separates classes with the widest margin. The kernel trick lets them draw nonlinear boundaries by implicitly mapping data into higher dimensions.

Concepts

  • Margin and support vectors: only the points closest to the boundary define it.
  • Soft margin and C: trades margin width against training errors; high C overfits.
  • Kernels: linear, polynomial and RBF. For RBF, gamma controls how local each point's influence is.
  • Hinge loss: the loss behind linear SVMs, comparable to log loss for logistic regression.
  • SVR applies the same ideas to regression.

When to use today

SVMs shine on small to medium datasets with many features (for example text or bioinformatics). Kernel SVMs scale poorly past roughly 100k samples, and gradient boosting usually wins on typical tabular data. They do not output probabilities natively.

Must-do

Scale features and tune C and gamma together on a log grid.

Good enough to move on when you can visualize decision boundaries for linear and RBF SVMs on a 2D toy dataset and explain the effect of C and gamma.

Key concepts

  • Maximum margin
  • Soft margin and C
  • Kernel trick and RBF
  • Hinge loss
  • Support vector regression

Mini project

Plot decision boundaries of linear, polynomial and RBF SVMs on scikit-learn's make_moons data for a grid of C and gamma values, then apply the best setup to the digits dataset.

Check yourself

What does a very large gamma do to an RBF SVM's decision boundary?

Free resources