Created
March 16, 2019 14:29
-
-
Save wangz10/43006ddc81de1a91dd6fc0f81b77322f to your computer and use it in GitHub Desktop.
Perform SVD with numpy
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 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