Shahzad
Shahzad

Reputation: 2049

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?

Upvotes: 4

Views: 1497

Answers (1)

lejlot
lejlot

Reputation: 66775

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

Upvotes: 2

Related Questions