Create custom Regressor in sklearn

Viewed 2611

I would like to use a custom Regressor with sklearn GridSearchCV. The spirit of it is analogous to creating a custom Classifier - as shown in this blog:

http://danielhnyk.cz/creating-your-own-estimator-scikit-learn/

Here is a condensed/edited portion of the code from the blog - showing some of the required methods to override.

from sklearn.base import BaseEstimator, RegressorMixin

class MyCustomClassifier(BaseEstimator, RegressorMixin):  
    """An example of classifier"""

    def __init__(self, intValue=0, stringParam="defaultValue", otherParam=None):
        """
        Called when initializing the classifier
        """
        self.intValue = intValue
        self.stringParam = stringParam

    def fit(self, X, y=None):
        self.treshold_ = (sum(X)/len(X)) + self.intValue  # mean + intValue

        return self

    def _meaning(self, x):
        # returns True/False according to fitted classifier
        # notice underscore on the beginning
        return( True if x >= self.treshold_ else False )

    def predict(self, X, y=None):
         ..

The things that would be same between Regressor and Classifier would likely be the __init__, fit, predict methods: I can probably piece that much together. But what about equivalents to __meaning ? Or other subtle considerations for a Regressor that might not be obvious/clear?

Here is a skeleton I am starting with:

from sklearn.base import BaseEstimator, RegressorMixin
class MyCustomRegressor(BaseEstimator, RegressorMixin):
    def __init__(self, regressorMethod):
      self.regressorMethod = regressorMethod

    def fit(self, X, y=None):
      self.regressorMethod(X,y)
      return self

It is my understanding that the score method will be used by GridSearchCv ..?

0 Answers
Related