Reputation: 55
I would like to do a grid-search through cross-validation for a custom kernel SVM using scikit-learn. More precisely following this example I want to define a kernel function like
def my_kernel(x, y):
"""
We create a custom kernel:
k(x, y) = x * M *y.T
"""
return np.dot(np.dot(x, M), y.T)
where M is a parameter of the kernel (like the gamma in the gaussian kernel).
I want to feed this parameter M through GridSearchCV, with something like
parameters = {'kernel':('my_kernel'), 'C':[1, 10], 'M':[M1,M2]}
svr = svm.SVC()
clf = grid_search.GridSearchCV(svr, parameters)
So my question is : how to define the my_kernel so that the M variable will be given by GridSearchCV ?
Upvotes: 3
Views: 891
Reputation: 2487
You may have to make a wrapper class. Something like:
class MySVC(BaseEstimator,ClassifierMixin):
def __init__( self,
# all the SVC attributes
M ):
self.M = M
# etc...
def fit( self, X, y ):
kernel = lambda x,y : np.dot(np.dot(x,M),y.T)
self.svc_ = SVC( kernel=kernel, # the other parameters )
return self.svc_.fit( X, y )
def predict( self, X ):
return self.svc_.predict( X )
# et cetera
Upvotes: 1