ag487 / matrix-eigenvectors

Calculates eigenvectors & eigenvalues from scratch, and as a case study decomposes an image into its principal components.
Other
1 stars 1 forks source link

Inconsistent signs in comparison to `np.linalg.eig()` #1

Closed RahulBhalley closed 3 years ago

RahulBhalley commented 3 years ago

Hi! Thank you so much for this implementation. Couldn't find any other repository this great anywhere!

import numpy as np
from utilities import matrix, eigen

x_cov = ...
Q1, H = matrix.hessenberg(x_cov)
Q2, eig_vals = eigen.decomposition(H)
eig_vecs = np.matmul(Q1, Q2)

evals, evecs = np.linalg.eig(x_cov)

for i in range(5):
  print(evecs[i])
  print(eig_vecs[i], "\n")

Prints the following. Notice the inconsistency in signs in column indices 1 and 4. Is this a good or bad sign if I use your implementation in any ML algorithm?

[-0.25480228 -0.28179571  0.89969481  0.17451349  0.1255439 ]
[-0.25480228  0.28179572  0.89969479  0.17485887 -0.12506255] 

[-0.48512613 -0.62431147 -0.42943828  0.23125432  0.37012435]
[-0.48512613  0.6243115  -0.42943834  0.23227362 -0.36948541] 

[ 0.62009866 -0.48148726  0.06583177 -0.48477065  0.37988323]
[ 0.62009866  0.48148729  0.0658317  -0.48372173 -0.38121794] 

[-0.07167955  0.54270901  0.02758864  0.02808775  0.83592997]
[-0.07167955 -0.54270895  0.02758849  0.03039174 -0.83584942] 

[-0.55683762  0.06680808  0.03220345 -0.82478784 -0.06447099]
[-0.55683762 -0.06680809  0.03220347 -0.8249624   0.06219735]

Looking towards your reply.

Regards Rahul Bhalley

RahulBhalley commented 3 years ago

Sorry, my understanding might be limited in computing eigenvectors and eigenvalues. This implementation seems to be working fine aside from that it's slow (but at least it is correct!).

Thanks a lot for making this repository!

Regards Rahul Bhalley

ag487 commented 3 years ago

Rahul, thanks for the kind comments. Agreed, Python is slow.