Dữ liệu nằm trên manifold cong trong không gian gốc — PCA tuyến tính không tách được cấu trúc đó. Kernel PCA đưa dữ liệu lên không gian feature cao hơn (implicitly) rồi áp PCA. Bài này theo Chương 5 Python Machine Learning — phần Kernel PCA intuition.
Mục tiêu
- Hiểu giới hạn PCA tuyến tính và động cơ kernel trick.
- Nắm intuition: kernel matrix thay cho dot product trong không gian feature.
- Biết các kernel phổ biến (
rbf,poly,sigmoid) và hyperparameter cơ bản.
Lý thuyết
Giới hạn PCA tuyến tính
PCA chỉ tìm subspace tuyến tính — tổ hợp tuyến tính của feature gốc. Nếu dữ liệu có cấu trúc vòng tròn (two moons, Swiss roll), các PC tuyến tính không "làm phẳng" manifold — clustering hay visualization trên 2 PC đầu kém ý nghĩa.
Kernel trick (tóm tắt)
Giả sử ánh xạ $\phi: \mathbb{R}^d \to \mathcal{H}$ đưa dữ liệu lên không gian Hilbert sao cho trong $\mathcal{H}$ cấu trúc gần tuyến tính hơn. PCA trong $\mathcal{H}$ cần dot product $\phi(\mathbf{x}_i)^T \phi(\mathbf{x}_j)$ — kernel $K(\mathbf{x}_i, \mathbf{x}_j) = \phi(\mathbf{x}_i)^T \phi(\mathbf{x}_j)$ tính được mà không cần $\phi$ explicit.
Kernel PCA thực hiện eigen-decomposition trên ma trận kernel $n \times n$ thay vì hiệp phương sai $d \times d$ — hiệu quả khi $n \ll d$ hoặc khi chỉ cần transform training set.
Ma trận kernel và centering
Với training samples $\mathbf{x}_1, \ldots, \mathbf{x}n$, ma trận $K{ij} = k(\mathbf{x}_i, \mathbf{x}_j)$. Trước PCA trong feature space, kernel phải centered:
$$\tilde{K} = K - \mathbf{1}_n K - K \mathbf{1}_n + \mathbf{1}_n K \mathbf{1}_n$$
(sklearn thực hiện nội bộ khi fit). Eigenvector của $\tilde{K}$ cho các principal components trong kernel space.
Kernel phổ biến
| Kernel | Công thức | Ghi chú |
|---|---|---|
| RBF (Gaussian) | $\exp(-\gamma |\mathbf{x}-\mathbf{x}'|^2)$ | Phổ biến nhất; gamma điều độ độ cục bộ |
| Polynomial | $(\gamma \mathbf{x}^T \mathbf{x}' + r)^d$ | Tương tự feature poly |
| Sigmoid | $\tanh(\gamma \mathbf{x}^T \mathbf{x}' + r)$ | Không phải lúc nào cũng positive definite |
Tài liệu: KernelPCA.
So sánh PCA vs Kernel PCA
| PCA | Kernel PCA | |
|---|---|---|
| Subspace | Tuyến tính gốc | Phi tuyến (implicit) |
| Chi phí fit | $O(\min(n d^2, n^2 d))$ SVD | $O(n^3)$ eigen trên $K$ |
| Transform điểm mới | Đơn giản: $\mathbf{x}^T \mathbf{V}$ | Cần kernel với tất cả training points |
| Hyperparameters | Ít | kernel, gamma, degree, ... |
Ứng dụng
- Visualization dữ liệu phi tuyến (2D embedding).
- Denoising, feature extraction trước clustering.
- Không phải lúc nào cũng beat PCA + model phi tuyến (RF, kernel SVM) — cân nhắc pipeline end-to-end.
Code Python
So sánh PCA tuyến tính và Kernel PCA (RBF) trên dữ liệu moons tổng hợp:
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import make_moons
from sklearn.decomposition import PCA, KernelPCA
from sklearn.preprocessing import StandardScaler
X, y = make_moons(n_samples=300, noise=0.08, random_state=42)
X_std = StandardScaler().fit_transform(X)
pca = PCA(n_components=2)
X_pca = pca.fit_transform(X_std)
kpca = KernelPCA(n_components=2, kernel="rbf", gamma=10, fit_inverse_transform=False)
X_kpca = kpca.fit_transform(X_std)
fig, axes = plt.subplots(1, 2, figsize=(10, 4))
for ax, X_proj, title in zip(
axes, [X_pca, X_kpca], ["Linear PCA", "Kernel PCA (RBF)"]
):
ax.scatter(X_proj[:, 0], X_proj[:, 1], c=y, cmap="coolwarm", s=20)
ax.set_title(title)
plt.tight_layout()
plt.savefig("pca_vs_kpca_moons.png", dpi=100)
print("Linear PCA — moons classes overlap on PC1? check plot.")
print("Kernel PCA lambdas (first 5):", kpca.eigenvalues_[:5])
Với gamma phù hợp, Kernel PCA thường tách hai nửa moon rõ hơn trên 2 thành phần đầu.
Lỗi thường gặp
- Chọn
gammaquá lớn/nhỏ: RBF quá cục bộ → overfitting kernel matrix; quá rộng → gần PCA tuyến tính. - Quên scale feature trước RBF: Khoảng cách Euclidean bị lệch nếu scale khác nhau — dùng
StandardScaler. - Kỳ vọng transform nhanh trên dữ liệu lớn: Kernel PCA fit $O(n^3)$ — không phù hợp $n > 10^4$ không có approximation.
- Nhầm Kernel PCA với t-SNE/UMAP: Kernel PCA là linear PCA trong feature space — không tối ưu local neighborhood như t-SNE.
Bài tập
- Tự luận: Vì sao ma trận kernel cần centered tương tự centering dữ liệu trong PCA? (Trả lời intuitive 3–4 câu.)
- Thực hành: Trên
make_moons, thửgamma=1, 10, 100. Vẽ 3 scatter và mô tả ảnh hưởng của gamma. - Đọc docs: Liệt kê kernel được hỗ trợ trong
KernelPCAvà tham số bắt buộc củapoly.
Tóm tắt
Kernel PCA mở rộng PCA sang cấu trúc phi tuyến bằng kernel trick — eigen-analysis trên ma trận Gram thay vì hiệp phương sai. Intuition: "làm phẳng" manifold trong feature space cao chiều. Chi phí và tuning gamma là trade-off chính so với PCA tuyến tính.
Tiếp tục lộ trình tại khóa Machine Learning cơ bản — bài tiếp theo: ví dụ Kernel PCA bằng Python chi tiết.