Sub topic 14
Beyond Plain PCA: Kernel, Sparse, Incremental and Nonlinear Methods
This chapter assumes you can fit PCA in scikit-learn, read explained variance, and know that PCA finds linear directions of maximum variance from the centered data. Each method below changes one assumption of plain PCA.
What plain PCA assumes
Plain PCA assumes three things:
- The structure in the data is linear.
- All the data fits in memory.
- Each component may mix every original feature.
When one of these fails, you can switch to a variant.
Kernel PCA: curved structure
Kernel PCA applies PCA in an implicit high-dimensional feature space. It never builds that space. It works with a kernel matrix of pairwise similarities between samples, such as the RBF kernel. Two concentric circles are the classic failure case for linear PCA, because no straight line separates them.
from sklearn.datasets import make_circles
from sklearn.decomposition import PCA, KernelPCA
from sklearn.linear_model import LogisticRegression
X, y = make_circles(n_samples=400, factor=0.3, noise=0.05, random_state=0)
linear = PCA(n_components=2).fit_transform(X)
kernel = KernelPCA(n_components=2, kernel="rbf", gamma=10).fit_transform(X)
for name, Z in [("linear PCA", linear), ("RBF kernel PCA", kernel)]:
clf = LogisticRegression().fit(Z[:, :1], y)
print(name, clf.score(Z[:, :1], y))Using only the first component, linear PCA scores near chance. The RBF version should separate the rings almost perfectly.
Kernel PCA has costs:
- The kernel matrix is n by n, so memory and time grow quickly with the number of samples.
- You must tune
gamma. - Reconstruction is not exact, because a point in feature space has no guaranteed preimage in the original space.
Incremental PCA: data that does not fit in memory
IncrementalPCA updates its estimate one batch at a time with partial_fit. It approximates plain PCA without ever holding the full matrix.
import numpy as np
from sklearn.decomposition import IncrementalPCA
rng = np.random.default_rng(0)
ipca = IncrementalPCA(n_components=5)
for _ in range(10):
batch = rng.normal(size=(500, 20))
ipca.partial_fit(batch)
print(ipca.explained_variance_ratio_)Each batch must contain at least n_components rows. You can also feed it a memory-mapped array through fit and set batch_size.
Sparse PCA: readable components
Plain loadings are usually dense, so every feature contributes a little to every component. SparsePCA adds an L1 penalty that pushes many loadings to exactly zero.
from sklearn.datasets import load_digits
from sklearn.decomposition import SparsePCA
X, _ = load_digits(return_X_y=True)
spca = SparsePCA(n_components=3, alpha=1.0, random_state=0)
spca.fit(X)
print((spca.components_ == 0).mean())The printed value is the fraction of zero loadings. Raising alpha increases sparsity.
Sparse components are not orthogonal and do not maximize variance in order. The explained variance ratios from plain PCA do not carry over.
Nonlinear methods
Manifold methods such as t-SNE and UMAP preserve neighborhoods instead of variance. They are mainly for visualization.
- Their axes have no loadings.
- Distances between far-apart clusters are not reliable.
- Most implementations cannot project new points consistently.
Autoencoders learn a nonlinear encoder and decoder. An autoencoder with linear layers and squared error recovers the same subspace as PCA.
Summary of choices
| Method | Fixes | Main cost |
|---|---|---|
| Kernel PCA | Curved structure | n by n kernel matrix, tuning `gamma` |
| Incremental PCA | Data larger than memory | Approximate result |
| Sparse PCA | Hard-to-read loadings | Components are not orthogonal |
| t-SNE or UMAP | Cluster visualization | No loadings, unreliable global distances |
| Autoencoder | Flexible nonlinear mapping | Training and tuning effort |
Exercise
You have 50 million rows and 100 features, and the matrix does not fit in RAM. You want a linear projection that you can also apply to new data later. Which method fits, and why not t-SNE?
Show answer
Use IncrementalPCA, fitted with partial_fit on batches. It gives a linear projection that you can apply to new rows with transform. t-SNE does not scale to this size, and most implementations cannot project new points consistently.