PIXELBANKv9.1.0
Menu

Implement Kernel PCA using the RBF kernel.

Standard PCA finds linear projections. Kernel PCA applies the kernel trick to find nonlinear projections:

  1. Compute the RBF kernel matrix: Kij=exp⁡(−∥xi−xj∥2/(2σ2))K_{ij} = \exp(-\|x_i - x_j\|^2 / (2\sigma^2))
  2. Center the kernel matrix: K~=K−1nK−K1n+1nK1n\tilde{K} = K - \mathbf{1}_n K - K \mathbf{1}_n + \mathbf{1}_n K \mathbf{1}_n where 1n\mathbf{1}_n is the n×nn \times n matrix with all entries 1/n1/n
  3. Compute eigenvalues/eigenvectors of K~\tilde{K}
  4. Return the top kk eigenvector columns scaled by λ\sqrt{\lambda}

For simplicity, this problem uses n=3n=3 points and k=1k=1. Return the projected values rounded to 4 decimal places.

Note: Use power iteration to find the dominant eigenvector.

Example:

Input:
X = [[0], [1], [2]]
sigma = 1.0
k = 1
Output:
[[-0.6575], [0.0], [0.6575]]
Reasoning:
  • The RBF kernel matrix KK is computed using the formula Kij=exp⁡(−∥xi−xj∥2/(2σ2))K_{ij} = \exp(-\|x_i - x_j\|^2 / (2\sigma^2)), resulting in a 3×33 \times 3 matrix.
  • The kernel matrix is then centered to obtain K~=K−1nK−K1n+1nK1n\tilde{K} = K - \mathbf{1}_n K - K \mathbf{1}_n + \mathbf{1}_n K \mathbf{1}_n, where 1n\mathbf{1}_n is a 3×33 \times 3 matrix with all entries 1/31/3.
  • Using power iteration, the dominant eigenvector of K~\tilde{K} is found, and its components are scaled by the square root of the corresponding eigenvalue λ\sqrt{\lambda}.
  • The resulting vector is then rounded to 4 decimal places, yielding the projected values [−0.6575,0.0,0.6575][-0.6575, 0.0, 0.6575].

Constraints:

  • X: 2D list of data points (n x d)
  • sigma: RBF kernel width
  • k: number of components (1)
  • Return 2D list (n x k) of projected values, rounded to 4 decimal places
🔒

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test Results

0/0
Run code to see test results.