Goal: finding the optimal hyperplane that maximally separates the datapoints of different classes.

  • For linearly separable data, this means finding the hyperplane that maximizes the margin between two classes.
  • The margin is the distance between the hyperplane and the closest data points from each class.

Hyperplane:

  • in an -dimensional space, a hyperplane is defined as:

  • The decision rule becomes

  • The support vectors are the datapoints closest to the decision boundary.

  • They define the margin, and the optimal hyperplane is fully determined by these points.

  • Removing non-support vectors has no effect on the decision boundary.

Margin is: We want to maximize the margin, which is equivalent to minimizing

The Primal

With labels , the hard-margin SVM is the constrained quadratic program:

Soft Margin C

Real data is rarely separable, so we allow violations via slack variables :

The penalty trades margin width against training errors:

Effect
Large few violations, narrow margin, risk of overfitting
Small wider margin, more violations, more regularized

Hinge Loss

Eliminating the slacks turns the soft-margin problem into unconstrained regularized empirical risk minimization with the hinge loss:

The hinge is zero once a point is correctly classified with margin at least 1, so only margin-violating points contribute gradient.

The Dual and Kernels

The Lagrangian dual depends on the data only through inner products :

Only support vectors have . Because only inner products appear, we can replace with a kernel to fit nonlinear boundaries without ever forming the high-dimensional features. See Kernel Methods. Common choices: RBF and polynomial kernels.

sklearn

from sklearn.svm import SVC
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
 
clf = make_pipeline(
    StandardScaler(),
    SVC(kernel="rbf", C=1.0, gamma="scale"),
)
clf.fit(X_train, y_train)
print(clf.score(X_test, y_test))

Hinge Loss in torch

import torch
 
def hinge_loss(scores, y, w, C=1.0):     # y in {-1, +1}
    margins = torch.clamp(1 - y * scores, min=0)
    return 0.5 * (w @ w) + C * margins.mean()

Pitfalls

  • SVMs are scale-sensitive; always standardize features (the RBF kernel especially).
  • Classic SVM solvers scale poorly past ~100k samples; consider linear SVM with SGD (hinge loss) for large data.
  • gamma and C interact strongly; tune them jointly by cross-validation.
  • Kernel Methods generalize the kernel trick beyond SVMs.