# sklearn SVM custom kernel

I need to implement a custom kernel in sklearn.

This would be a custom linear kernel:

def my_kernel(x, y): return np.dot(x, y.T)

But I am having trouble doing something like RBF kernel. Is it possible to do that in sklearn a custom kernel?

I have tried this:

def my_kernel(x, y): gamma = 0.01 return np.exp((gamma* np.power(np.linalg.norm(x-y),2)))`

But did not work.

(I know that there is a pre-implementation of RBF, but I need to manually implement it, because I need to add some parameters)

## Answers

Your function looks good. Just use

clf = svm.SVC(kernel=my_kernel) clf.fit(X, Y)

There is an example related to your application.

I have implemented somthing like this

import numpy as np from sklearn.metrics.pairwise import euclidean_distances def gaussian_kernel(X, Y): kernel = euclidean_distances(X, Y) ** 2 kernel = kernel*(-1/(self.gamma**2)) kernel = np.exp(kernel) return kernel

and then have called svm with my defined kernel

from sklearn import svm clf = svm.SVC(kernel=gaussian_kernel, max_iter = 10000) clf.fit(X_train, y_train)

this seemed to work fine. The only this that I failed to do was pass a hyperparameter from svm to my implementation of kernel, so I started defining the hyper-parameter globally instead as a work around.

As you would see I have defined a self.gamma which I wanted to tune but the gamma defining while initializing svm was not passing to my function.