Skip to content

Instantly share code, notes, and snippets.

@andersx
Created November 27, 2019 12:03
Show Gist options
  • Save andersx/17f7c88cf897d9040d46bf2daa747a54 to your computer and use it in GitHub Desktop.
Save andersx/17f7c88cf897d9040d46bf2daa747a54 to your computer and use it in GitHub Desktop.
SVD example
101
102 eigen_vecs, eigen_vals, Vh = \
103 np.linalg.svd(X.T, full_matrices=False, compute_uv=True)
104
105 print("svd done")
106
107 print(eigen_vals.shape)
108 print(eigen_vecs.shape)
109
110 NPCA = 5000
111
112 print(eigen_vals[NPCA-1])
113
114 cev = 100 - (np.sum(eigen_vals) - np.sum(eigen_vals[:NPCA])) / np.sum(eigen_vals) * 100
115 print("Cumulative explained variance = %f %%" % cev )
116
117 X = np.einsum("jk,kl->jl", X, eigen_vecs[:,:NPCA])
118 Xs = np.einsum("jk,kl->jl", Xs, eigen_vecs[:,:NPCA])
119
120 print(X.shape)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment