Inherit from scikit-learn LassoCV model

I tried to extend the scikit-learn RidgeCV model using inheritance:

from sklearn.linear_model import RidgeCV, LassoCV class Extended(RidgeCV): def __init__(self, *args, **kwargs): super(Extended, self).__init__(*args, **kwargs) def example(self): print 'Foo' x = [[1,0],[2,0],[3,0],[4,0], [30, 1]] y = [2,4,6,8, 60] model = Extended(alphas = [float(a)/1000.0 for a in range(1, 10000)]) model.fit(x,y) print model.predict([[5,1]]) 

It worked fine, but when I tried to inherit LassoCV, it got the following trace:

 Traceback (most recent call last): File "C:/Python27/so.py", line 14, in <module> model.fit(x,y) File "C:\Python27\lib\site-packages\sklearn\linear_model\coordinate_descent.py", line 1098, in fit path_params = self.get_params() File "C:\Python27\lib\site-packages\sklearn\base.py", line 214, in get_params for key in self._get_param_names(): File "C:\Python27\lib\site-packages\sklearn\base.py", line 195, in _get_param_names % (cls, init_signature)) RuntimeError: scikit-learn estimators should always specify their parameters in the signature of their __init__ (no varargs). <class '__main__.Extended'> with constructor (<self>, *args, **kwargs) doesn't follow this convention. 

Can someone explain how to fix this?

+5
source share
1 answer

You will probably want to make a model compatible with scikit-learn, use it further with the available scikit-learn functionality. If you do this, you need to read this first: http://scikit-learn.org/stable/developers/contributing.html#rolling-your-own-estimator

In short: scikit-learn has many functions, such as grade cloning (the clone () function), meta-algorithms such as GridSearch , Pipeline , cross-validation. And all this should be able to get the values โ€‹โ€‹of the fields inside your grade and change the value of these fields (for example, GridSearch should change the parameters inside your grade before each grade), for example the alpha parameter in SGDClassifier . To change the value of a parameter, he must know his name. To get the names of all the fields in each class method get_params from BaseEstimator class (which you inherit implicitly), it is required that all parameters be specified in the __init__ method for the class, since it easily introspectively looks at all __init__ parameter names (Look at BaseEstimator , this is the class that calls this error).

So he just wants you to delete all varargs, for example

 *args, **kwargs 

from __init__ . You must list all the parameters of your model in the __init__ signature and initialize all the internal fields of the object.

Here is an example of the __init__ SGDClassifier method, which inherits from BaseSGDClassifier :

 def __init__(self, loss="hinge", penalty='l2', alpha=0.0001, l1_ratio=0.15, fit_intercept=True, n_iter=5, shuffle=True, verbose=0, epsilon=DEFAULT_EPSILON, n_jobs=1, random_state=None, learning_rate="optimal", eta0=0.0, power_t=0.5, class_weight=None, warm_start=False, average=False): super(SGDClassifier, self).__init__( loss=loss, penalty=penalty, alpha=alpha, l1_ratio=l1_ratio, fit_intercept=fit_intercept, n_iter=n_iter, shuffle=shuffle, verbose=verbose, epsilon=epsilon, n_jobs=n_jobs, random_state=random_state, learning_rate=learning_rate, eta0=eta0, power_t=power_t, class_weight=class_weight, warm_start=warm_start, average=average) 
+5
source

Source: https://habr.com/ru/post/1258157/


All Articles