RBF Kernel PCA From Scratch
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: medium
Interview Round: Other
Implement RBF kernel PCA for the training points using NumPy. Write three functions:
- `rbf_kernel(X, Y, gamma)` returns the `(n, m)` Gram matrix with entries `exp(-gamma * ||x - y||^2)`.
- `center_kernel(K)` returns the double-centred matrix `K - 1K - K1 + 1K1`, where `1 = ones(n, n) / n`.
- `kernel_pca(X, n_components, gamma)` returns `(scores, eigenvalues, alphas)` for the training points.
With `k = n_components`, the outputs follow these conventions:
- `eigenvalues` has shape `(k,)` and holds the top `k` eigenvalues of the centred Gram matrix (not divided by `n`), in descending order.
- `alphas` has shape `(n, k)` and holds the matching unit-norm eigenvectors, each column sign-flipped so that its largest-absolute entry is positive.
- `scores` has shape `(n, k)` and equals `alphas * sqrt(max(eigenvalues, 0))`.
Example:
```text
X = [[0.0], [1.0]], n_components = 1, gamma = 0.7
eigenvalues = [1 - exp(-0.7)] # ≈ [0.50341]
alphas ≈ [[0.7071], [-0.7071]]
scores ≈ [[0.5017], [-0.5017]] # sqrt(0.50341 / 2)
```
```hint Centring without big products
Multiplying a matrix by `ones(n, n) / n` on the left or on the right replaces each entry with a mean. Work out which mean each of the four terms is.
```
```hint From eigenvectors to scores
Write the projection of a training point onto a feature-space component, then use the eigenvector equation of the centred Gram matrix to simplify it.
```
```hint Solver output
Check the order in which `np.linalg.eigh` returns eigenvalues, and what rounding can do to the smallest ones.
```
### Constraints & Assumptions
- `X` is a finite float array of shape `(n, d)` with `n ≥ 2`.
- `1 ≤ n_components ≤ n` and `gamma > 0`.
- Results must match scikit-learn's `KernelPCA` (dense solver) up to the stated sign convention, to about `1e-6`.
- If two entries of a column tie for the largest absolute value (as in the example), the entry with the lower row index decides the sign.
- Only the training points need to be projected. Transforming unseen points is out of scope.
### What a Strong Answer Covers
- A vectorised squared-distance computation for the RBF kernel that guards against small negative values from rounding.
- Double-centring computed from row, column and grand means, with a reason why it equals centring the data in feature space.
- A symmetric eigensolver, descending order, the top `n_components`, and the sign convention applied deterministically.
- Why the training scores equal each eigenvector times the square root of its eigenvalue, and how near-zero or negative eigenvalues are handled.
- Time and memory cost in terms of `n` and `d`.
### Follow-up Questions
- How would you project a new point onto the learned components?
- How would you choose `gamma`, and what happens to the centred Gram matrix when `gamma` is very large or very small?
- With `n = 100,000`, the dense `n x n` matrix and its eigendecomposition no longer fit. What would you change?
- Why does the centred Gram matrix always have a zero eigenvalue, and what does that mean for `n_components = n`?
Overview: Implement RBF kernel PCA in NumPy: build the Gram matrix, double-centre it, and eigendecompose it to return scores, eigenvalues and sign-fixed eigenvectors for the training points. Tests vectorised kernel code, feature-space centring and how kernel PCA scores follow from the eigenvectors.