Created
March 11, 2019 10:32
-
-
Save hemalvarambhia/620711e22c9ad01d6d55a0530cc28c18 to your computer and use it in GitHub Desktop.
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
import numpy as np | |
from numpy.linalg import inv | |
from scipy import linalg | |
from sklearn.decomposition import PCA | |
from sklearn import utils | |
X = np.array([[-1, -1], [-2, -1], [-3, -2], [1, 1], [2, 1], [3, 2]]) | |
mean_ = np.mean(X, axis=0) | |
X = X - mean_ | |
U, S, V = linalg.svd(X, full_matrices=False) | |
print('U=', U) | |
print('V=', V) | |
# Reverse engineering the algorithm: | |
max_abs_cols = np.argmax(np.abs(U), axis=0) | |
print('max_abs_cols') | |
print(max_abs_cols) | |
submatrix = U[max_abs_cols, range(U.shape[1])] | |
print('submatrix = ', submatrix) | |
signs = np.sign(submatrix) | |
print('signs =', signs) | |
print('U flipped = ', U * signs) | |
print('V flipped = ', V * signs[:, np.newaxis]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment