Neural Sync Active
Kernel Methods
Registry Synced
Kernel Methods
549 words
3 min read
Reading compass
Now ยท ๐ฏ Learning Objectives
Kernel Methods
๐ฏ Learning Objectives
- Explain the kernel trick and why it's computationally efficient
- Implement kernelized SVM with RBF kernel
- Understand Mercer's theorem and valid kernels
- Use kernel PCA for non-linear dimensionality reduction
๐ Core Content
4.1 Intuition: The Kernel Trick
Some problems that aren't linearly separable become separable in higher dimensions. But explicitly mapping data to high dimensions is expensive. The kernel trick computes dot products in the transformed space without ever computing the transformation.
4.2 Mathematical Formulation
Feature map: ฯ:RnโRd, where dโซn (often d=โ).
Kernel function: K(x,xโฒ)=ฯ(x)Tฯ(xโฒ)
Mercer's theorem: A symmetric function K(x,xโฒ) is a valid kernel iff the kernel matrix Kijโ=K(xiโ,xjโ) is positive semi-definite for all finite sets {xiโ}.
4.3 Common Kernels
| Kernel | Formula | Feature Space | Use |
|---|---|---|---|
| Linear | xTxโฒ | Same | Baseline |
| Polynomial | (ฮณxTxโฒ+r)d | All monomials up to degree d | Images |
| RBF (Gaussian) | $\exp(-\gamma\ | x - x'\ | ^2)$ |
| Sigmoid | tanh(ฮณxTxโฒ+r) | Neural network-like | Certain text problems |
4.4 The Representer Theorem
For kernel methods, the optimal weight vector can be expressed as:
So the decision function becomes:
This is the "kernel trick": we only need K(xiโ,x), never ฯ(xiโ).
4.5 Kernelized SVM
The dual formulation only uses kernel evaluations:
Subject to 0โคฮฑiโโคC, โฮฑiโyiโ=0
python# runnable from sklearn.svm import SVC from sklearn.datasets import make_circles from sklearn.model_selection import train_test_split # Non-linear data X, y = make_circles(n_samples=300, noise=0.1, factor=0.5, random_state=42) X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42) # RBF kernel svm_rbf = SVC(kernel='rbf', C=10, gamma=1.0) svm_rbf.fit(X_train, y_train) print(f"RBF SVM accuracy: {svm_rbf.score(X_test, y_test):.3f}") # Compare with linear kernel svm_linear = SVC(kernel='linear', C=10) svm_linear.fit(X_train, y_train) print(f"Linear SVM accuracy: {svm_linear.score(X_test, y_test):.3f}")
4.6 Kernel PCA
python# runnable from sklearn.decomposition import KernelPCA from sklearn.datasets import make_circles X, y = make_circles(n_samples=200, noise=0.05, random_state=42) kpca = KernelPCA(n_components=2, kernel='rbf', gamma=10) X_kpca = kpca.fit_transform(X) print(f"Original shape: {X.shape}, KPCA shape: {X_kpca.shape}") print(f"Explained variance ratio: {kpca.eigenvalues_}")
๐ Practice Questions
Q1: What makes a valid kernel function?Mercer's theorem: K(x,xโฒ) is a valid kernel if for any finite set of points, the kernel matrix Kijโ=K(xiโ,xjโ) is positive semi-definite (all eigenvalues โฅ 0). This ensures there exists some feature map ฯ such that K(x,xโฒ)=ฯ(x)Tฯ(xโฒ). Q2: Why is RBF kernel infinite-dimensional?The RBF kernel exp(โฮณโฅxโxโฒโฅ2) can be expanded as a Taylor series: exp(โฮณโฅxโxโฒโฅ2)=โn=0โโn!(ฮณ)nโ(xTxโฒ)nEach term (xTxโฒ)n corresponds to a feature space of polynomial degree n. The sum over all n means the implicit feature space is a sum of all polynomial feature spaces โ infinite-dimensional. Q3: When does the kernel trick save computation?If ฯ(x) maps to a very high (or infinite) dimensional space, computing ฯ(x) explicitly is impossible. The kernel trick computes K(xiโ,xjโ)=ฯ(xiโ)Tฯ(xjโ) directly without computing ฯ. For RBF, this is just exp(โฮณโฅxiโโxjโโฅ2) โ O(n) time per pair, regardless of the infinite-dimensional feature space. Join Discord PreviousNeural NetworksNextTime Series