Implement a Nystroem Low-Rank Feature Map for the RBF Kernel

Quick Overview

Implement an RBF kernel and a Nystroem feature map that picks seeded landmarks and builds low-rank features whose inner products approximate the full Gram matrix. It tests kernel approximation, inverse square roots computed with an SVD, numerical stability on near-singular matrices, and complexity analysis in NumPy.

Implement a Nystroem Low-Rank Feature Map for the RBF Kernel

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Implement `rbf_kernel(X, Y, gamma)`, which returns `K[i, j] = exp(-gamma * ||X[i] - Y[j]||^2)`, and the class `NystroemRBF(n_components, gamma, seed)`. Its `fit` picks landmarks, and its `transform` returns features `F` such that `F @ F.T` approximates the RBF Gram matrix. The implementation must follow these rules: - `fit` takes the landmark indices as `np.random.default_rng(seed).permutation(n)[:n_components]`. Store them as `landmark_indices_` and the corresponding rows as `components_`. - `fit` stores `normalization_`, the inverse square root `K_mm^(-1/2)` of the landmark Gram matrix, computed via SVD with singular values clamped to at least `1e-12`, and returns `self`. - `transform(X)` returns `rbf_kernel(X, components_, gamma) @ normalization_.T`, with shape `(n_samples, n_components)`. - `fit` raises `ValueError` when `n_components` is outside `[1, n_samples]`. - With `n_components == n_samples`, `F @ F.T` reproduces the full Gram matrix (up to floating-point error). Example: ```python rbf_kernel([[0, 0], [1, 0]], [[0, 0], [1, 1]], gamma=0.5) # [[1.0, exp(-1.0)], # [exp(-0.5), exp(-0.5)]] ``` ```hint All pairs at once NumPy broadcasting over a new axis gives every pairwise difference in one expression. Work out how much memory that temporary needs. ``` ```hint What the normalization must satisfy You need a matrix `M` with `M.T @ M` equal to the inverse of the landmark Gram matrix. One decomposition of that Gram matrix gives you `M`, and it also shows where a tiny singular value would cause trouble. ``` ```hint A self-check Fit with `n_components` equal to the number of samples and compare `F @ F.T` with `rbf_kernel(X, X, gamma)`. ``` ### Constraints & Assumptions - `X` is a finite float array of shape `(n, d)`, and `gamma > 0`. - Use NumPy only. Results must match scikit-learn's RBF kernel to `rtol=1e-7`. ### What a Strong Answer Covers - How `rbf_kernel` computes all pairwise squared distances, and what that costs in memory. - Landmark selection that follows the seeded permutation exactly, and the attributes `fit` stores. - How `normalization_` comes from the landmark Gram matrix, and why the singular values are clamped. - Why `F @ F.T` approximates the Gram matrix, and why it is exact when every sample is a landmark. - Time and space complexity of `fit` and `transform`. ### Follow-up Questions - How would you choose landmarks better than uniform sampling? - How does the approximation error change as `n_components` grows, and how would you pick its value? - Why use a clamped SVD instead of inverting `K_mm` directly or taking its Cholesky factor? - How would you compute the kernel for large `n` without materializing an `(n, m, d)` array?

Overview: Implement an RBF kernel and a Nystroem feature map that picks seeded landmarks and builds low-rank features whose inner products approximate the full Gram matrix. It tests kernel approximation, inverse square roots computed with an SVD, numerical stability on near-singular matrices, and complexity analysis in NumPy.

General logo
General
Nov 24, 2026
mediumMachine Learning EngineerOtherMachine Learning
0
0

Implement rbf_kernel(X, Y, gamma), which returns K[i, j] = exp(-gamma * ||X[i] - Y[j]||^2), and the class NystroemRBF(n_components, gamma, seed). Its fit picks landmarks, and its transform returns features F such that F @ F.T approximates the RBF Gram matrix.

The implementation must follow these rules:

  • fit takes the landmark indices as np.random.default_rng(seed).permutation(n)[:n_components] . Store them as landmark_indices_ and the corresponding rows as components_ .
  • fit stores normalization_ , the inverse square root K_mm^(-1/2) of the landmark Gram matrix, computed via SVD with singular values clamped to at least 1e-12 , and returns self .
  • transform(X) returns rbf_kernel(X, components_, gamma) @ normalization_.T , with shape (n_samples, n_components) .
  • fit raises ValueError when n_components is outside [1, n_samples] .
  • With n_components == n_samples , F @ F.T reproduces the full Gram matrix (up to floating-point error).

Example:

rbf_kernel([[0, 0], [1, 0]], [[0, 0], [1, 1]], gamma=0.5)
# [[1.0,       exp(-1.0)],
#  [exp(-0.5), exp(-0.5)]]

Constraints & Assumptions

  • X is a finite float array of shape (n, d) , and gamma > 0 .
  • Use NumPy only. Results must match scikit-learn's RBF kernel to rtol=1e-7 .

What a Strong Answer Covers Guidance

  • How rbf_kernel computes all pairwise squared distances, and what that costs in memory.
  • Landmark selection that follows the seeded permutation exactly, and the attributes fit stores.
  • How normalization_ comes from the landmark Gram matrix, and why the singular values are clamped.
  • Why F @ F.T approximates the Gram matrix, and why it is exact when every sample is a landmark.
  • Time and space complexity of fit and transform .

Follow-up Questions Guidance

  • How would you choose landmarks better than uniform sampling?
  • How does the approximation error change as n_components grows, and how would you pick its value?
  • Why use a clamped SVD instead of inverting K_mm directly or taking its Cholesky factor?
  • How would you compute the kernel for large n without materializing an (n, m, d) array?