Cross validation for custom SVM kernel in scikit-learn
I would like to do grid search through cross validation for a custom SVM core 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 kernel parameter (for example, gamma in a Gaussian kernel).
I want to pass this M parameter via 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 do I define my_kernel so that the M variable is set by the GridSearchCV?
+3
source to share
1 answer
You may need to create 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
0
source to share