Skip to content

Instantly share code, notes, and snippets.

@absent1706
Created April 25, 2019 16:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save absent1706/985f10d5dac2f7c4a1572cfd206d3b0b to your computer and use it in GitHub Desktop.
Save absent1706/985f10d5dac2f7c4a1572cfd206d3b0b to your computer and use it in GitHub Desktop.
svd-from-scratch
# 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