I am trying fit different kernels to calculate similarity matrix in R. Here is example data - X matrix :

X <- matrix(rep(0,200*1000),200,1000)
for (i in 1:200) {
X[i,] <- ifelse(runif(1000)<0.5,-1,1)

What kernels can be used to generate a similarity matrix and how can I do this in R ?



"kernel methods are a class of algorithms for pattern analysis (see wikipedia).The general task of pattern analysis is to find and study general types of relations (for example clusters, rankings, principal components, correlations, classifications) in datasets. For many of these tasks, data have to be represented as feature vectors, but kernel methods replace this representation by similarities to other data points."

" Kernel methods owe their name to the use of kernel functions.which enable them to operate in a high-dimensional, implicit feature space without ever computing the coordinates of the data in that space, but rather by simply computing the inner products between the images of all pairs of data in the feature space. This operation is often computationally cheaper than the explicit computation of the coordinates. This approach is called the kernel trick. Kernel functions have been introduced for sequence data, graphs, text, images, as well as vectors".

Gaussian Kernel

Given two D dimensional vectors xi and xj . The Gaussian kernel is defined as:

enter image description here


enter image description here

is is the Euclidean distance and and $\sigma$$^2$ is the bandwidth of the kernel. The the Gaussian kernel is a measure of similarity between xi and xj . It evalues to 1 if the xi and xj are identical, and approaches 0 as xi and xj move further apart. The function gausskernel from package KRLS calculate such similarity matrix.


The package kernellab has many kernels and discussions. The function kcca in this package can perform Kernel Canonical Correlation Analysis which include different Kernels -

rbfdot Radial Basis kernel function "Gaussian"

polydot Polynomial kernel function

vanilladot Linear kernel function

tanhdot Hyperbolic tangent kernel function

laplacedot Laplacian kernel function

besseldot Bessel kernel function

anovadot ANOVA RBF kernel function

splinedot Spline kernel


## initialize kernel function
rbf <- rbfdot(sigma = 0.05)
## calculate kernel matrix
kernelMatrix(rbf, X)

I am not sure how many of these kernel measures can be used as similarity measure. The package also perform Kernel Principal Components Analysis (function - kha).

Also see: How Good is a Kernel When Used as a Similarity Measure?


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.