Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

kernel methods in Python

Referring to https://stats.stackexchange.com/questions/15798/how-to-calculate-a-gaussian-kernel-effectively-in-numpy , a solution is provided for calculating the precomputed kernel matrix.

from scipy.spatial.distance import pdist, squareform
X = loaddata() # this is an NxD matrix, where N is number of items and D its dimensions
pairwise_dists = squareform(pdist(X, 'euclidean'))
K = scip.exp(pairwise_dists / s**2)

How one can implement the above Guassin kernel, if the input is a weighted adjacency matrix for a directed graph?

like image 426
Shahzad Avatar asked Mar 07 '26 12:03

Shahzad


1 Answers

If you already have your distance matrix, you could simply apply

K = scip.exp(YOUR_DISTANCE_HERE / s**2)

However, it may no longer be a kernel. Not all "similarity scores" are valid kernels. If your distances is a valid Mahalanobis distance then you have a guarantee, that everything will be ok. In case of "any" distance - anything can happen.

Usinig invalid kernel may lead to:

  1. Optimization process crash
  2. Finding suboptimal solutions (bad solutions)
  3. Doing anything, there are completely no guarantees.

Maybe you should consider graph kernels which are somewhat strongly related to the gaussian kernel and the heat diffusion

like image 129
lejlot Avatar answered Mar 09 '26 00:03

lejlot