I am trying to transform an array using its spectral components.
Let's consider an 4x4 array arr
It will have an adjacent matrix A, a degree matrix D and Laplacian matrix L = D-A
import numpy as np
from numpy.linalg import eig
Compute its eigenvectors and eigenvalues, and sort it in descending order
eig_val, eig_vec = eig(L)
idx = eig_val.argsort()[::-1]
Sort the eigenvectors accordingly
eig_vec = eig_vec[:,idx]
The product of 2 distincts eigenvectors must be 0. I notice that this is not the case here e.g. the product of the first and second eigenvectors is:
sum(np.multiply(eig_vec[0], eig_vec[1])) = 0.043247527085787975
Is there anything I am missing ?
Compute the spectral components of the input array
spectral = np.matmul(eig_vec.transpose(), arr.flatten())
print(spectral.shape)
Take the first 15 components.
masked = np.zeros(spectral.shape)
m = spectral[:15]
masked[:15] = m
Get the new features
updated_arr = np.matmul(eig_vec, masked)
updated_arr = updated_arr.reshape(4, -1)
The updated array is very different from the original.
Any suggestion or resource to have a look at will be welcome.
To get the eigendecomposition of a symmetric matrix, you should use the
eighfunction instead ofeig. The following code works for me:The fact is that your
Lmatrix has repeated eigenvalues (for example the largest eigenvalue has multiplicity of 2) and so your eigenvectors do not have to be necessarily orthogonal (as any linear combination of eigenvectors related to the same eigenvalue is also an eigenvector). And in this caseeigdoes not guarantee returning you orthogonal eigenvectors. You can test it:Unfortunately, the documentation of
eighdoes not promise you orthogonality either, however it mentions that it uses thelapackfunction_syevdfor real matrices of which documentation do promise to return orthogonal eigenvectors. Aseigis designed to find the right eigenvectors of general (not necessary symmetric) matrices, it does not even try to orthogonalize them (it uses thelapackroutine_geevof which documentation does not mention orthogonalization).