Skip to main content

Lesson 4 of 9

Support Vector Machines (SVM): Hyperplanes, Margin Maximization & Kernel Trick

Support Vector Machines (SVM) seek the unique decision boundary hyperplane w^T x + b = 0 that maximizes the geometric margin 2 / ||w|| between classes. Hard-margin SVM requires linear separability; soft-margin SVM introduces slack variables ξ_i and regularization parameter C to tolerate non-separable overlap. For non-linear data, the Kernel Trick implicitly projects data into an infinite-dimensional Hilbert space via kernel inner products K(x, z) = <φ(x), φ(z)> without ever computing the explicit high-dimensional mapping.

25 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

Linear vs Non-Linear Radial Basis Function (RBF) Kernels

Linear vs Non-Linear Radial Basis Function (RBF) Kernels
import numpy as np
import pandas as pd
from sklearn.datasets import make_circles
from sklearn.svm import SVC
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import Pipeline
from sklearn.metrics import accuracy_score

# Generate non-linearly separable concentric circles
X, y = make_circles(n_samples=500, factor=0.4, noise=0.1, random_state=42)

# 1. LINEAR SVM (Cannot separate concentric circles)
linear_svm = Pipeline([
    ('scaler', StandardScaler()),
    ('svc', SVC(kernel='linear', C=1.0))
])
linear_svm.fit(X, y)
acc_linear = accuracy_score(y, linear_svm.predict(X))
print(f"Linear SVM Accuracy on Concentric Rings: {acc_linear*100:.2f}% (Fails due to non-linearity)")

# 2. RBF KERNEL SVM: K(x, x') = exp(-gamma * ||x - x'||^2)
# The RBF kernel computes similarity in an infinite-dimensional feature space
rbf_svm = Pipeline([
    ('scaler', StandardScaler()),
    ('svc', SVC(kernel='rbf', C=1.0, gamma='scale'))
])
rbf_svm.fit(X, y)
acc_rbf = accuracy_score(y, rbf_svm.predict(X))
support_vectors = rbf_svm.named_steps['svc'].support_vectors_

print(f"RBF Kernel SVM Accuracy               : {acc_rbf*100:.2f}% (Near-perfect non-linear separation!)")
print(f"Number of Support Vectors Identified  : {len(support_vectors)} out of {len(X)} samples")
print(f"Sparsity: Only {100*len(support_vectors)/len(X):.1f}% of data points govern the decision boundary!")

Pro Tip: Tuning the Gamma (γ) and C Hyperparameters

In RBF SVM, parameter `C` controls the margin penalty (large C = hard margin, penalizing slack heavily; small C = soft margin, allowing wider margin). Parameter `gamma` controls kernel radius (large gamma = narrow bell curves, fitting individual sample points tightly and risking severe overfitting; small gamma = broad smooth boundaries). Jointly tune `(C, gamma)` on a logarithmic grid `[0.1, 1, 10, 100]`.

Diagram
Loading diagram…

MlSVMAlgorithm Visual Representation

Hands-On Challenge: Polynomial Kernel vs RBF Comparison

Train an `SVC(kernel='poly', degree=2)` on the concentric circles dataset. Demonstrate why a 2nd-degree polynomial kernel can separate concentric circles algebraically: `x1^2 + x2^2 = r^2` becomes a straight line in quadratic space.

Quick Quiz & Key Concept Checks

Common Pitfall: Scaling Complexity with Large Datasets

Standard SVM training via the Sequential Minimal Optimization (SMO) algorithm scales between O(n²) and O(n³) with sample size. Training an `SVC(kernel='rbf')` on more than 50,000 rows will take hours or run out of memory. Use `LinearSVC` or SGD with Nyström kernel approximations for large datasets.

Topics in This Module

0/9 done