Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sort eigen vector in PrincipalComponentAnalysis #476

Closed
raytroop opened this issue Dec 3, 2018 · 1 comment
Closed

sort eigen vector in PrincipalComponentAnalysis #476

raytroop opened this issue Dec 3, 2018 · 1 comment

Comments

@raytroop
Copy link

raytroop commented Dec 3, 2018

def _decomposition(self, mat, n_samples):
if self.solver == 'eigen':
e_vals, e_vecs = np.linalg.eig(mat)
elif self.solver == 'svd':
u, s, v = np.linalg.svd(mat.T)
e_vecs, e_vals = u, s
e_vals = e_vals ** 2 / n_samples
sort_idx = np.argsort(e_vals)[::-1]
e_vals, e_vecs = e_vals[sort_idx], e_vecs[sort_idx]
return e_vals, e_vecs

Because the column v[:,i] is the eigenvector,
e_vals, e_vecs = e_vals[sort_idx], e_vecs[sort_idx]

should be

e_vals, e_vecs = e_vals[sort_idx], e_vecs[:, sort_idx]
@raytroop raytroop changed the title sort eigen vector sort eigen vector in PrincipalComponentAnalysis Dec 3, 2018
@rasbt
Copy link
Owner

rasbt commented Dec 3, 2018

Thanks! Looks like there eigenvectors were already sorted in all the unit tests which is why this wasn't caught in the unit tests. I think this is guaranteed in SVD but not necessarily in np.eig. Will fix it, many thanks!

@rasbt rasbt mentioned this issue Dec 3, 2018
5 tasks
@rasbt rasbt closed this as completed in #477 Dec 3, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants