Skip to content

Instantly share code, notes, and snippets.

@wangz10
Created March 16, 2019 14:29
Show Gist options
  • Select an option

  • Save wangz10/43006ddc81de1a91dd6fc0f81b77322f to your computer and use it in GitHub Desktop.

Select an option

Save wangz10/43006ddc81de1a91dd6fc0f81b77322f to your computer and use it in GitHub Desktop.
Perform SVD with numpy
def svd(X):
# Data matrix X, X doesn't need to be 0-centered
n, m = X.shape
# Compute full SVD
U, Sigma, Vh = np.linalg.svd(X,
full_matrices=False, # It's not necessary to compute the full matrix of U or V
compute_uv=True)
# Transform X with SVD components
X_svd = np.dot(U, np.diag(Sigma))
return X_svd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment