Skip to content

Instantly share code, notes, and snippets.

@amankharwal
Created February 20, 2021 09:39
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 amankharwal/d7eed2e3c9eb76a1ca61ef1eb52555be to your computer and use it in GitHub Desktop.
Save amankharwal/d7eed2e3c9eb76a1ca61ef1eb52555be to your computer and use it in GitHub Desktop.
X_centered = X - X.mean(axis=0)
U, s, Vt = np.linalg.svd(X_centered)
c1 = Vt.T[:, 0]
c2 = Vt.T[:, 1]
m, n = X.shape
S = np.zeros(X_centered.shape)
S[:n, :n] = np.diag(s)
np.allclose(X_centered, U.dot(S).dot(Vt))
W2 = Vt.T[:, :2]
X2D = X_centered.dot(W2)
print(X2D)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment