Skip to content

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

@vmgustavo

Description

@vmgustavo

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions