Cross validation for custom kernel SVM in scikit-learn -
i grid-search through cross-validation custom kernel svm using scikit-learn. more precisely following this example want define kernel function like
def my_kernel(x, y): """ create custom kernel: k(x, y) = x * m *y.t """ return np.dot(np.dot(x, m), y.t)
where m parameter of kernel (like gamma in gaussian kernel).
i want feed parameter m through gridsearchcv,
parameters = {'kernel':('my_kernel'), 'c':[1, 10], 'm':[m1,m2]} svr = svm.svc() clf = grid_search.gridsearchcv(svr, parameters)
so question : how define my_kernel m variable given gridsearchcv ?
you may have make wrapper class. like:
class mysvc(baseestimator,classifiermixin): def __init__( self, # 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, # other parameters ) return self.svc_.fit( x, y ) def predict( self, x ): return self.svc_.predict( x ) # et cetera
Comments
Post a Comment