Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@alextp
Created November 4, 2010 13:12
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save alextp/662433 to your computer and use it in GitHub Desktop.
Save alextp/662433 to your computer and use it in GitHub Desktop.
Gunnar Martinsson's fast svd
import numpy as np, numpy.linalg as linalg
def fast_svd(M, k):
p = k+5
Y = np.dot(M, np.random.normal(size=(M.shape[1],p)))
Q,r = linalg.qr(Y)
B = np.dot(Q.T,M)
Uhat, s, v = linalg.svd(B, full_matrices=False)
U = np.dot(Q, Uhat)
return U.T[:k].T, s[:k], v[:k]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment