Skip to content
E
Educatum

Non-linear SVM

Curated by Educatum AI teamLast updated:
Basic Concepts
Direct Answer

Non-linear Support Vector Machines extend linear SVMs to curved decision boundaries by using kernel functions such as the Radial Basis Function (RBF) or polynomial kernels. Via the kernel trick, the dual optimization computes inner products in a potentially infinite-dimensional feature space implicitly, avoiding explicit coordinate transformations.

Study Card: Non-linear SVM

Direct Answer

Non-linear SVMs handle datasets that are not linearly separable in their original input space by implicitly mapping points x↦ϕ(x)x \mapsto \phi(x) into a higher-dimensional (even infinite-dimensional) feature space where a linear separating hyperplane exists.

Instead of explicitly evaluating high-dimensional coordinates ϕ(x)\phi(x), the dual SVM formulation relies exclusively on pairwise inner products ⟨ϕ(xi),ϕ(xj)⟩\langle \phi(x_i), \phi(x_j) \rangle replaced by a positive semi-definite kernel function K(xi,xj)K(x_i, x_j)—such as the RBF (Gaussian) kernel:

K(x,x′)=exp⁡(−γ∥x−x′∥22)K(x, x') = \exp\big(-\gamma \|x - x'\|_2^2\big)

Predictions at inference time depend only on the support vectors (αi>0\alpha_i > 0):

f(x)=∑i∈SVαiyiK(xi,x)+bf(x) = \sum_{i \in \text{SV}} \alpha_i y_i K(x_i, x) + b

This enables expressive non-linear decision boundaries in the original space without explicit feature expansion.

Key Terms

  • Kernel Function: K(x,x′)=⟨ϕ(x),ϕ(x′)⟩K(x, x') = \langle \phi(x), \phi(x') \rangle, computing inner products in a potentially infinite-dimensional reproducing kernel Hilbert space (RKHS) directly from input vectors.
  • Kernel Trick: Substituting K(xi,xj)K(x_i, x_j) in the dual SVM objective and decision function so ϕ(x)\phi(x) never needs to be materialized explicitly.
  • Feature Mapping (ϕ\phi): Conceptual transformation from input space Rd\mathbb{R}^d to a higher-dimensional space where linear separation is feasible.
  • Hyperplane: The linear separating surface in the transformed feature space, corresponding to a curved non-linear boundary in input space.
  • Support Vectors: Training points with non-zero dual multipliers (αi>0\alpha_i > 0) that define the non-linear boundary.

Example

Consider classifying two interleaving crescent shapes (make_moons). No straight line in 2D can separate the crescents accurately. A non-linear SVM with an RBF kernel evaluates localized similarity exp⁡(−γ∥x−xi∥2)\exp(-\gamma \|x - x_i\|^2) around support vectors, creating a smooth curved decision boundary in 2D that corresponds to a linear hyperplane in the RBF kernel’s infinite-dimensional feature space.

Code Implementation

import numpy as np
import matplotlib.pyplot as plt
from sklearn import svm, datasets
from sklearn.model_selection import train_test_split

# Load a non-linearly separable dataset (make_moons)
X, y = datasets.make_moons(n_samples=100, noise=0.1, random_state=42)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4, random_state=42)

# Train a non-linear SVM classifier using the RBF kernel
clf = svm.SVC(kernel='rbf', gamma='scale', C=1.0)
clf.fit(X_train, y_train)
print(f"Test Accuracy: {clf.score(X_test, y_test):.4f}")

# Helper functions to visualize the 2D non-linear decision boundary
def make_meshgrid(x, y, h=0.02):
    x_min, x_max = x.min() - 1, x.max() + 1
    y_min, y_max = y.min() - 1, y.max() + 1
    return np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))

def plot_contours(ax, clf, xx, yy, **params):
    Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])
    Z = Z.reshape(xx.shape)
    return ax.contourf(xx, yy, Z, **params)

fig, ax = plt.subplots()
X0, X1 = X[:, 0], X[:, 1]
xx, yy = make_meshgrid(X0, X1)

plot_contours(ax, clf, xx, yy, cmap=plt.cm.coolwarm, alpha=0.8)
ax.scatter(X0, X1, c=y, cmap=plt.cm.coolwarm, s=20, edgecolors='k')
ax.set_xlabel('x1')
ax.set_ylabel('x2')
ax.set_xticks(())
ax.set_yticks(())
ax.set_title('Decision surface of non-linear SVM (RBF kernel)')
plt.show()
  • Common Kernels: RBF (e−γ∥x−x′∥2e^{-\gamma \|x-x'\|^2}), Polynomial ((γ⟨x,x′⟩+r)d(\gamma \langle x, x' \rangle + r)^d), and Linear (⟨x,x′⟩\langle x, x' \rangle). RBF is typically the default starting point for non-linear tabular problems after feature standardization.
  • Hyperparameters CC and γ\gamma: CC controls margin violation penalties; in RBF kernels, γ\gamma controls the influence radius of each support vector—large γ\gamma narrows the Gaussian kernel and creates tighter, potentially overfit islands around training points.
  • Mercer’s Condition: Ensures a candidate kernel matrix Kij=K(xi,xj)K_{ij} = K(x_i, x_j) is symmetric and positive semi-definite so the dual SVM optimization remains convex.
  • Computational Scaling: Kernel SVMs solve an nn-variable quadratic program with O(n2)O(n^2)–O(n3)O(n^3) training time and O(nSV⋅d)O(n_{\text{SV}} \cdot d) inference cost, making them slower than linear SVMs or tree ensembles on massive datasets (n≫105n \gg 10^5).
View all →

Try keywords like RoPE, FlashAttention, DPO, or Blind 75.