Created
April 25, 2019 16:36
-
-
Save absent1706/985f10d5dac2f7c4a1572cfd206d3b0b to your computer and use it in GitHub Desktop.
svd-from-scratch
This file contains 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
# this method works well on ALL data | |
# see https://towardsdatascience.com/my-notes-for-singular-value-decomposition-with-interactive-code-feat-peter-mills-7584f4f2930a | |
import numpy as np | |
from sklearn import preprocessing | |
np.set_printoptions(precision=2) | |
X = np.array([ | |
[1,2,3], | |
[4,10,1], | |
[5,3,2], | |
[8,1,9], | |
[5,6,9], | |
]) | |
scaler = preprocessing.StandardScaler() | |
X_std = scaler.fit_transform(X) | |
X_dot_XT = X_std.dot(X_std.T) | |
S,U = np.linalg.eig(X_dot_XT) | |
S = np.diag(np.sqrt(S)) | |
V = X_std.T.dot(U).dot(np.linalg.inv(S)) | |
reconstructed_X = np.dot(U,S).dot(V.T) | |
print('U\n',np.real(U)) | |
print() | |
print('S\n',np.real(S)) | |
print() | |
print('V\n',np.real(V)) | |
print('-----------') | |
print('X\n', X_std) | |
print() | |
print('U * S * VT\n', np.real(reconstructed_X)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment