pyGAM icon indicating copy to clipboard operation
pyGAM copied to clipboard

Can't clone LinearGAM estimator object for sklearn cross-validation

Open vmgustavo opened this issue 4 years ago • 7 comments

Bug

Can't use LinearGAM with ScikitLearn cross_validate as it get's a RuntimeError for not being able to clone the model.

The only estimator out of all the GAM options that can't be cloned is LinearGAM

To Reproduce

  • using cross_validate
from sklearn.model_selection import cross_validate

from pygam import LinearGAM, s
from pygam.datasets import toy_interaction

X, y = toy_interaction(return_X_y=True)

gam = LinearGAM(s(0, by=1))

cross_validate(gam, X, y, cv=5, scoring='neg_mean_absolute_error')
~/miniconda3/envs/sbid/lib/python3.8/site-packages/sklearn/model_selection/_validation.py in <genexpr>(.0)
    242     scores = parallel(
    243         delayed(_fit_and_score)(
--> 244             clone(estimator), X, y, scorers, train, test, verbose, None,
    245             fit_params, return_train_score=return_train_score,
    246             return_times=True, return_estimator=return_estimator,

~/miniconda3/envs/sbid/lib/python3.8/site-packages/sklearn/utils/validation.py in inner_f(*args, **kwargs)
     70                           FutureWarning)
     71         kwargs.update({k: arg for k, arg in zip(sig.parameters, args)})
---> 72         return f(**kwargs)
     73     return inner_f
     74 

~/miniconda3/envs/sbid/lib/python3.8/site-packages/sklearn/base.py in clone(estimator, safe)
     94         param2 = params_set[name]
     95         if param1 is not param2:
---> 96             raise RuntimeError('Cannot clone object %s, as the constructor '
     97                                'either does not set or modifies parameter %s' %
     98                                (estimator, name))

RuntimeError: Cannot clone object LinearGAM(callbacks=['deviance', 'diffs'], fit_intercept=True, 
   max_iter=100, scale=None, terms=s(0), tol=0.0001, verbose=False), as the constructor either does not set or modifies parameter callbacks
  • If only LinearGAM is not able to be cloned
from sklearn.base import clone

from pygam import LinearGAM, GammaGAM, InvGaussGAM, LogisticGAM, PoissonGAM, ExpectileGAM

for estimator in [LinearGAM, GammaGAM, InvGaussGAM, LogisticGAM, PoissonGAM, ExpectileGAM]:
    try:
        clone(estimator())
    except Exception as e:
        print(estimator.__name__, e.__class__.__name__)
LinearGAM RuntimeError
RuntimeError: Cannot clone object LinearGAM(callbacks=['deviance', 'diffs'], fit_intercept=True, 
   max_iter=100, scale=None, terms='auto', tol=0.0001, verbose=False), as the constructor either does not set or modifies parameter callbacks

Environment

Python 3.8.5 pygam==0.8.0

vmgustavo avatar Feb 18 '21 14:02 vmgustavo

Same error here!

lucasmazim avatar Mar 30 '22 23:03 lucasmazim

I am having this issue as well

thistleknot avatar Apr 30 '22 02:04 thistleknot

Working on this right now.

It seems like this has to do with a small bug in the __init__ method of LinearGAM in which the callbacks parameter is not getting passed when calling super().__init__().

I tried the examples shared by @vmgustavo and got no runtime errors:

Example 1


from sklearn.model_selection import cross_validate
from pygam import LinearGAM, s
from pygam.datasets import toy_interaction

X, y = toy_interaction(return_X_y=True)

gam = LinearGAM(s(0, by=1))

cross_validate(gam, X, y, cv=5, scoring='neg_mean_absolute_error')

Example 2


from sklearn.base import clone

from pygam import LinearGAM, GammaGAM, InvGaussGAM, LogisticGAM, PoissonGAM, ExpectileGAM

for estimator in [LinearGAM, GammaGAM, InvGaussGAM, LogisticGAM, PoissonGAM, ExpectileGAM]:
    try:
        clone(estimator())
    except Exception as e:
        print(estimator.__name__, e.__class__.__name__)

Environment

Python 3.6.13 scikit-learn 0.24.2

Issue seems to be fixed so I will submit PR shortly.

miguelfmc avatar Aug 08 '22 04:08 miguelfmc

Just noticed that there was an open PR addressing this as well as the mutable default callbacks values:

#267

miguelfmc avatar Aug 08 '22 05:08 miguelfmc

from sklearn.model_selection import cross_validate from pygam import LinearGAM, s from pygam.datasets import toy_interaction X, y = toy_interaction(return_X_y=True) gam = LinearGAM(s(0, by=1)) cross_validate(gam, X, y, cv=5, scoring='neg_mean_absolute_error')

It doesn't work.

Cannot clone object LinearGAM(callbacks=['deviance', 'diffs'], fit_intercept=True, max_iter=100, scale=None, terms=s(0), tol=0.0001, verbose=False), as the constructor either does not set or modifies parameter callbacks

Cain-Iriving avatar Dec 06 '23 15:12 Cain-Iriving