Non-linear SVM
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 into a higher-dimensional (even infinite-dimensional) feature space where a linear separating hyperplane exists.
Instead of explicitly evaluating high-dimensional coordinates , the dual SVM formulation relies exclusively on pairwise inner products replaced by a positive semi-definite kernel function —such as the RBF (Gaussian) kernel:
Predictions at inference time depend only on the support vectors ():
This enables expressive non-linear decision boundaries in the original space without explicit feature expansion.
Key Terms
- Kernel Function: , computing inner products in a potentially infinite-dimensional reproducing kernel Hilbert space (RKHS) directly from input vectors.
- Kernel Trick: Substituting in the dual SVM objective and decision function so never needs to be materialized explicitly.
- Feature Mapping (): Conceptual transformation from input space 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 () 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 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()
Related Concepts
- Common Kernels: RBF (), Polynomial (), and Linear (). RBF is typically the default starting point for non-linear tabular problems after feature standardization.
- Hyperparameters and : controls margin violation penalties; in RBF kernels, controls the influence radius of each support vector—large narrows the Gaussian kernel and creates tighter, potentially overfit islands around training points.
- Mercer’s Condition: Ensures a candidate kernel matrix is symmetric and positive semi-definite so the dual SVM optimization remains convex.
- Computational Scaling: Kernel SVMs solve an -variable quadratic program with – training time and inference cost, making them slower than linear SVMs or tree ensembles on massive datasets ().