Skip to content

Instantly share code, notes, and snippets.

View isayev's full-sized avatar
👨‍💻
busy writing proposals

Olexandr Isayev isayev

👨‍💻
busy writing proposals
View GitHub Profile
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,:]