Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to transform an array using its spectral components

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.

like image 1000
Kyv Avatar asked Oct 16 '25 19:10

Kyv


1 Answers

Answering the part about the orthogonal eigenvectors: np.linalg.eig returns the eigenvectors, such that the column eigenvectors[:,i] is the eigenvector corresponding to the eigenvalue eigenvalues[i]. So calculating the product like this

sum(np.multiply(eig_vec[:,0], eig_vec[:,1]))

the sum will be zero.

like image 112
SEMate Avatar answered Oct 18 '25 08:10

SEMate