python - how to use GridSearchCV with custom estimator in sklearn? -


i have estimator should compatible sklearn api. trying fit 1 parameter of estimator gridsearchcv not understand how it.

this code:

import numpy np import sklearn sk  sklearn.linear_model import linearregression, lassolarscv, ridgecv sklearn.linear_model.base import linearclassifiermixin, sparsecoefmixin, baseestimator   class elm(baseestimator):      def __init__(self, n_nodes, link='rbf', output_function='lasso', n_jobs=1, c=1):         self.n_jobs = n_jobs         self.n_nodes = n_nodes         self.c = c          if link == 'rbf':             self.link = lambda z: np.exp(-z*z)         elif link == 'sig':             self.link = lambda z: 1./(1 + np.exp(-z))          elif link == 'id':             self.link = lambda z: z         else:             self.link = link          if output_function == 'lasso':             self.output_function = lassolarscv(cv=10, n_jobs=self.n_jobs)         elif output_function == 'lr':             self.output_function = linearregression(n_jobs=self.n_jobs)          elif output_function == 'ridge':             self.output_function = ridgecv(cv=10)          else:             self.output_function = output_function          return        def h(self, x):          n, p = x.shape         xw = np.dot(x, self.w.t)         xw = xw + np.ones((n, 1)).dot(self.b.t)         return self.link(xw)      def fit(self, x, y, w=none):          n, p = x.shape         self.mean_y = y.mean()         if w == none:             self.w = np.random.uniform(-self.c, self.c, (self.n_nodes, p))         else:             self.w = w          self.b = np.random.uniform(-self.c, self.c, (self.n_nodes, 1))         self.h_train = self.h(x)         self.output_function.fit(self.h_train, y)          return self      def predict(self, x):         self.h_predict = self.h(x)         return self.output_function.predict(self.h_predict)      def get_params(self, deep=true):         return {"n_nodes": self.n_nodes,                  "link": self.link,                 "output_function": self.output_function,                 "n_jobs": self.n_jobs,                  "c": self.c}      def set_params(self, **parameters):         parameter, value in parameters.items():             setattr(self, parameter, value)    ### fit c parameter ###  x = np.random.normal(0, 1, (100,5)) y = x[:,1] * x[:,2] + np.random.normal(0, .1, 100)   gs = sk.grid_search.gridsearchcv(elm(n_nodes=20, output_function='lr'),                                   cv=5,                                   param_grid={"c":np.linspace(0.0001,1,10)},                                  fit_params={})  #gs.fit(x, y) # error 

there 2 problems within code:

  1. you didn't specify scoring argument gridsearchcv. seems doing regression, mean_squared_error option.

  2. your set_params doesn't return reference object itself. should add return self after for loop.

    as andreas mentioned, need redefine set_params , get_params in scikit-learn. having inherited baseestimator should enough.


Comments

Popular posts from this blog

tcpdump - How to check if server received packet (acknowledged) -