[Python] Inherit from scikit-learn's LassoCV model

Discussão em 'Python' iniciado por Stack, Maio 8, 2025.

  1. Stack

    Stack Membro Participativo

    I tried to extend scikit-learn's 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 perfectly fine, but when I tried to inherit from LassoCV, it yielded the following traceback:

    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 somebody explain how to fix this?

    Continue reading...

Compartilhe esta Página