Skip to content

Support Vector Machines (SVM) ​

Maximum margin classifiers - margins, kernels, soft margin


One-Sentence Description ​

SVM finds the hyperplane that maximizes the margin between classes, with kernel tricks enabling non-linear decision boundaries.


Core Concept: Maximum Margin ​

What is Margin? ​

The margin is the distance between the decision boundary and the nearest data points from either class (support vectors).

Linear SVM with Maximum Margin

Why Maximize Margin? ​

BenefitExplanation
Better generalizationMore room for error on new data
Unique solutionMaximum margin hyperplane is unique
RobustnessLess sensitive to small perturbations

Support Vectors ​

  • Data points closest to the decision boundary
  • They "support" (define) the boundary
  • Key insight: Only support vectors matter - other points can be removed without changing the solution

Mathematical Formulation ​

Hard Margin SVM (Linearly Separable Data) ​

Objective: Find w, b that maximize margin =2∥w∥

Optimization Problem:

minw,b12∥w∥2subject to: yi(w⋅xi+b)≥1∀i

This is a convex quadratic programming problem with a unique solution.

Soft Margin SVM (Non-Separable Data) ​

Allow some misclassification via slack variables ξi:

minw,b,ξ12∥w∥2+C∑iξisubject to: yi(w⋅xi+b)≥1−ξi,ξi≥0

Soft Margin SVM

C Parameter Effect:

C ValueMarginErrorsRisk
Large CSmallerFewerOverfitting
Small CLargerMoreUnderfitting

Kernel Trick ​

The Problem ​

Linear SVM can only find linear decision boundaries. For non-linearly separable data, we need something more powerful.

The Solution ​

  1. Map data to higher-dimensional space: ϕ(x)
  2. Data may become linearly separable in higher dimensions
  3. Use kernel trick to avoid computing ϕ(x) explicitly

Kernel Trick Comparison

Kernel Function ​

K(xi,xj)=ϕ(xi)⋅ϕ(xj)

Computes dot product in high-dimensional space without explicitly transforming data.

Common Kernels ​

KernelFormulaUse Case
LinearK(x,y)=x⋅yLinearly separable data
PolynomialK(x,y)=(γx⋅y+r)dPolynomial boundaries
RBF/GaussianK(x,y)=exp⁡(−γ|x−y|2)Non-linear (most common)
SigmoidK(x,y)=tanh⁡(γx⋅y+r)Neural network-like

RBF Kernel Parameter γ ​

γ ValueInfluenceBoundaryRisk
LargeNarrowComplexOverfitting
SmallWideSmoothUnderfitting

Hyperparameter Tuning ​

Key Parameters ​

ParameterDescriptionTuning Range
CRegularization strength[0.01,0.1,1,10,100]
kernelDecision boundary type'rbf', 'linear', 'poly'
γRBF kernel width'scale', 'auto', [0.01,0.1,1]
degreePolynomial kernel degree[2,3,4,5]

Tuning Strategy ​

python
from sklearn.model_selection import GridSearchCV
from sklearn.svm import SVC

param_grid = {
    'C': [0.1, 1, 10, 100],
    'gamma': ['scale', 'auto', 0.1, 1, 10],
    'kernel': ['rbf', 'poly']
}

grid_search = GridSearchCV(SVC(), param_grid, cv=5, scoring='accuracy')
grid_search.fit(X_train, y_train)
print(f"Best params: {grid_search.best_params_}")

Common Pitfalls ​

  • Always scale features - SVM uses distances, sensitive to scale
  • Start with RBF kernel - Works well in most cases
  • Use cross-validation for C and γ - Default values rarely optimal

Key Properties ​

StrengthsWeaknesses
Effective in high dimensions (d>n)Slow for large n: O(n2) to O(n3)
Memory efficient (only stores SVs)Requires feature scaling
Versatile (different kernels)No native probability output
Robust to outliers (soft margin)Kernel selection is difficult
Strong theoretical guaranteesHard to interpret with kernels

Complexity Analysis ​

OperationTime ComplexitySpace Complexity
TrainingO(n2) to O(n3)O(n2)
PredictionO(nsv×d)O(nsv×d)

For large datasets, consider:

  • Linear SVM (LinearSVC) - O(n×d)
  • SGD classifier with hinge loss
  • Approximations (Nystrom, random features)

SVM vs Other Classifiers ​

AspectSVMLogistic Regression
ObjectiveMaximize marginMaximize likelihood
ProbabilitiesNot nativeNatural
KernelsEasy to applyRequires explicit features
ScalingCriticalLess sensitive
Large datasetsSlowerFaster

When to Use SVM ​

  • High-dimensional data (text, genomics)
  • nfeatures>nsamples
  • Non-linear boundaries needed
  • Medium-sized datasets (< 100K samples)

When to Avoid SVM ​

  • Large datasets (use linear SVM or SGD)
  • Need probability estimates (use logistic regression)
  • Interpretability required (use linear models or trees)

Interview Questions ​

Q1: "Explain what the kernel trick does." ​

The kernel trick lets us compute dot products in a high-dimensional feature space without explicitly transforming data there.

SVM only needs dot products between data points, never the transformed features themselves. A kernel function K(xi,xj)=ϕ(xi)⋅ϕ(xj) computes this directly, even when ϕ maps to infinite dimensions (like RBF).

Example: The RBF kernel K(x,y)=exp⁡(−γ∥x−y∥2) implicitly maps to infinite dimensions but computes in O(d) time.

Q2: "What's the role of C in SVM?" ​

C controls the trade-off between:

  1. Maximizing the margin
  2. Minimizing classification errors
  • Large C: Prioritizes correct classification, smaller margin, risk of overfitting
  • Small C: Prioritizes large margin, tolerates errors, better generalization

Tune C using cross-validation, typically searching powers of 10: [0.01,0.1,1,10,100]

Q3: "What are support vectors and why do they matter?" ​

Support vectors are training points on or within the margin boundary.

Why they matter:

  1. Define the model - Decision boundary depends only on SVs
  2. Sparse solution - Only store SVs for prediction
  3. Complexity indicator - Many SVs suggests complex boundary or noisy data

Example: 10,000 training points but only 500 SVs means prediction uses only those 500 points.

Q4: "How would you handle a large dataset with SVM?" ​

Standard SVM is O(n2) to O(n3). For large datasets:

  1. Linear SVM - LinearSVC is O(n×d) using coordinate descent
  2. SGD with hinge loss - SGDClassifier(loss='hinge') scales to millions
  3. Kernel approximations - Random Fourier features or Nystrom approximation
  4. Subsampling - Train on a subset (loses information)

Start with linear SVM or SGD, then try kernel approximations if non-linear boundaries are necessary.


Code Reference ​

python
import numpy as np
from sklearn.svm import SVC
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import GridSearchCV

# Always scale features for SVM
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)

# Basic SVM
svm = SVC(kernel='rbf', C=1.0, gamma='scale')
svm.fit(X_train_scaled, y_train)

# With probability estimates
svm_prob = SVC(kernel='rbf', probability=True)
svm_prob.fit(X_train_scaled, y_train)
probs = svm_prob.predict_proba(X_test_scaled)

# Hyperparameter tuning
param_grid = {'C': [0.1, 1, 10, 100], 'gamma': [0.01, 0.1, 1, 'scale']}
grid_search = GridSearchCV(SVC(kernel='rbf'), param_grid, cv=5)
grid_search.fit(X_train_scaled, y_train)

# Access support vectors
print(f"Number of support vectors: {len(svm.support_vectors_)}")
print(f"Support vector indices: {svm.support_}")

Quick Reference Card ​

SVM Essentials
GoalMaximize margin between classes
Decision Boundaryw⋅x+b=0
Margin2|w|
Soft Margin
Objectivemin12|w|2+C∑iξi
Large CFit training data, small margin
Small CLarge margin, allow errors
Kernels
LinearK(x,y)=x⋅y
PolynomialK(x,y)=(γx⋅y+r)d
RBFK(x,y)=exp⁡(−γ|x−y|2)
Complexity
TrainingO(n2) to O(n3)
PredictionO(nsv×d)
Use WhenAvoid When
High-dimensional data (d>n)Large datasets
Non-linear boundaries neededNeed probability estimates
Medium-sized datasetsInterpretability required