-
-
Save isayev/8ea7bcbbb682ae53bce2 to your computer and use it in GitHub Desktop.
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
| 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