Skip to content

Instantly share code, notes, and snippets.

@cwidmer
Last active August 29, 2015 13:56
Show Gist options
  • Save cwidmer/9283521 to your computer and use it in GitHub Desktop.
Save cwidmer/9283521 to your computer and use it in GitHub Desktop.
perform pca via eigendecomposition
def dual_pca(X, max_num_pcs):
"""
assuming N << D (X.shape[0] << X.shape[1]),
we first compute the kernel matrix K = XX^T, perform an
eigendecomposition and subsequently reconstruct
truncated principle components
"""
K = X.dot(X.T)
S, U = np.linalg.eigh(K)
S = S[::-1]
U = U[:,::-1]
# reduce dimensionality
U = U[:,0:max_num_pcs]
S = S[0:max_num_pcs]
# reconstruct principle components
PCs = - U * np.sqrt(S)
# compare to scikits (which uses SVD)
from sklearn.decomposition import PCA
pca = PCA(n_components=max_num_pcs)
PC2 = pca.fit_transform(G)
# PCs and PC2 will be the same except for differences in sign
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment