Created
March 5, 2024 20:34
-
-
Save dtxe/775f25d1a9f50095c722b01947c6e911 to your computer and use it in GitHub Desktop.
Kernel Trick PCA
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import Tuple | |
import numpy as np | |
def kt_pca(X:np.ndarray) -> Tuple[np.ndarray, np.ndarray]: | |
""" | |
This is an implementation of the linear kernel PCA method ("kernel trick") | |
described in "Kernel PCA Pattern Reconstruction via Approximate Pre-Images" | |
by Schölkopf et al, ICANN, 1998, pp 147-15 | |
Parameters | |
---------- | |
X : np.ndarray | |
Matrix of data in (T x N). Only need this trick if T>>N | |
Returns | |
------- | |
E : np.ndarray (T x N) | |
Eigenvectors of the kernel matrix, in descending order | |
S : np.ndarray (1 x N) | |
Eigenvalues of the kernel matrix, in descending order | |
""" | |
S2, F = np.linalg.eigh(np.matmul(X.T, X)) | |
F = F[:,::-1] | |
S2 = S2[::-1] | |
S = np.sqrt(S2) | |
ES = np.matmul(X,F) | |
E = ES/S | |
return E, S |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment