Reputation: 339
I'm trying to use Pipeline and GridSearchCV with a custom regression function.
I've got the approach to work using sklearn.svm.SVR by setting up its class, and then passing the parameters from the dictionary when setting up GridSearchCV.
I don't seem to be able to do the same thing when using my own regression function (RegFn). In particular I want to pass the values from the parameter dictionary to the RegFn when setting it up.
Grateful for any pointers on how to get this working.
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import GridSearchCV
from sklearn.pipeline import Pipeline
from sklearn.base import BaseEstimator,RegressorMixin
parameters = dict(regfn__i=i, regfn__j=j, regfn__k=k)
regfn = RegFn(numInputs=noinputs, numOutputs=1, i = 100, j = 1, k =0.5)
scaler = StandardScaler()
steps = [ ('scaler', scaler), ('regfn', regfn ) ]
grid = GridSearchCV(Pipeline(steps), param_grid=parameters, cv=splits, refit=True, verbose=3, n_jobs=1)
Updated class structure
class RBF(BaseEstimator,RegressorMixin):
def __init__(self, i,j,k,numInputs=10,numOutputs=1):
self.numInputs = numInputs
self.numOutputs = numOutputs
self.i = i
self.j = j
self.k = k
.....
def fit(self, x, y):
....
def predict(self, x):
....
I'm still not able to pass the i, j, k values from the dictionary. Am I referring to them in the correct way?
Upvotes: 1
Views: 1352
Reputation: 210972
If you want to use a custom estimator - it should be inherited from sklearn.base.BaseEstimator and it should implement methods fit()
and predict()
.
If you want to be able to pass parameters to your estimator you should also implement __init__()
method.
>>> import numpy as np
>>> from sklearn.base import BaseEstimator, RegressorMixin
>>> from sklearn.utils.validation import check_X_y, check_array, check_is_fitted
>>> from sklearn.utils.multiclass import unique_labels
>>> from sklearn.metrics import euclidean_distances
>>> class TemplateClassifier(BaseEstimator, RegressorMixin):
...
... def __init__(self, demo_param='demo'):
... self.demo_param = demo_param
...
... def fit(self, X, y):
...
... # Check that X and y have correct shape
... X, y = check_X_y(X, y)
... # Store the classes seen during fit
... self.classes_ = unique_labels(y)
...
... self.X_ = X
... self.y_ = y
... # Return the classifier
... return self
...
... def predict(self, X):
...
... # Check is fit had been called
... check_is_fitted(self, ['X_', 'y_'])
...
... # Input validation
... X = check_array(X)
...
... closest = np.argmin(euclidean_distances(X, self.X_), axis=1)
... return self.y_[closest]
PS please be aware that the example above shows how to create a custom classifier, so it might contain a code, which is not quite appropriate for regressors
Upvotes: 2