Created
December 23, 2013 12:31
-
-
Save julienr/8096321 to your computer and use it in GitHub Desktop.
PCA using scipy.sparse.linalg.svds (economy SVD decomposition)
This file contains 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
def pca(X, npc): | |
n_samples, n_features = X.shape | |
Xmean = np.mean(X, axis=0) | |
U, s, Vt = scipy.sparse.linalg.svds(X - Xmean, k=npc) | |
order = np.argsort(-s) # sort s in descending order | |
# svds returns U, s, Vt sorder in ascending order. We want descending | |
s = s[order] | |
W = Vt[order,:] | |
explained_variance = (s**2) / float(n_samples) | |
return Xmean, W, explained_variance |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment