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, 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)


Your function looks good. Just use

clf = svm.SVC(kernel=my_kernel), 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), 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.

Need Your Help

Why doesn't DerelictGL3 draw anything?

d opengl-3

I realize that I am not using Glu, nor setting the perspective, but I am using the same normalized device coordinates as McKeeson uses, so I should see the same

replacing value of XML file in javascript

javascript xml xml-parsing xmlhttprequest

I load an XML document using XMLHTTPRequest in my js file. the XML file is like below: