Skip to content

Feature: Support for Scikit-Learn v1.7+ Compatibility #422

@RHammond2

Description

@RHammond2

It seems that for scikit-learn>=1.7 there is a new requirement to implement __sklearn_tags__. I came across this version pinning workaround from an XGBoost issue for continuing to use scikit-learn and PyGAM together in the meantime. The change to scikit-learn means that any model training functionality cannot be run with PyGAM models for the latest releases of scikit-learn. I don't believe this is an immediate need, but it would be great if it could be addressed at some point.

Setup specifics

Failing Environment settings

  • Python 3.14
  • scikit-learn>1.6.1
  • pygam>=0.11

Successful workaround environment settings

  • Python 3.14
  • scikit-learn<1.7
  • pygam>=0.11

In-code call

The failing setup can boiled down to the following, but comes from OpenOA.

import numpy as np
from pygam import GAM
from sklearn.metrics import r2_score, make_scorer
from sklearn.model_selection import KFold, RandomizedSearchCV

scorer = make_scorer(r2_score, greater_is_better=True)
random_search = RandomizedSearchCV(
    GAM,
    cv=KFold,
    param_distributions={"n_splines": np.arange(5, 40)},
    n_iter=20,
    scoring=scorer,
    verbose=0,
    return_train_score=True,
)
random_search.fit(X, y)  # X and y 

Relevant traceback

estimator = GAM(callbacks=['deviance', 'diffs'], distribution='normal', 
   fit_intercept=True, link='identity', max_iter=100, terms='auto', 
   tol=0.0001, verbose=False)

    def get_tags(estimator) -> Tags:
        """Get estimator tags.
    
        :class:`~sklearn.BaseEstimator` provides the estimator tags machinery.
    
        For scikit-learn built-in estimators, we should still rely on
        `self.__sklearn_tags__()`. `get_tags(est)` should be used when we
        are not sure where `est` comes from: typically
        `get_tags(self.estimator)` where `self` is a meta-estimator, or in
        the common checks.
    
        .. versionadded:: 1.6
    
        Parameters
        ----------
        estimator : estimator object
            The estimator from which to get the tag.
    
        Returns
        -------
        tags : :class:`~.sklearn.utils.Tags`
            The estimator tags.
        """
    
        try:
            tags = estimator.__sklearn_tags__()
        except AttributeError as exc:
            if "object has no attribute '__sklearn_tags__'" in str(exc):
                # Happens when `__sklearn_tags__` is implemented by calling
                # `super().__sklearn_tags__()` but there is no `__sklearn_tags__`
                # method in the base class. Typically happens when only inheriting
                # from Mixins.
    
>               raise AttributeError(
                    f"The following error was raised: {exc}. It seems that "
                    "there are no classes that implement `__sklearn_tags__` "
                    "in the MRO and/or all classes in the MRO call "
                    "`super().__sklearn_tags__()`. Make sure to inherit from "
                    "`BaseEstimator` which implements `__sklearn_tags__` (or "
                    "alternatively define `__sklearn_tags__` but we don't recommend "
                    "this approach). Note that `BaseEstimator` needs to be on the "
                    "right side of other Mixins in the inheritance order."
                )
E               AttributeError: The following error was raised: 'GAM' object has no attribute '__sklearn_tags__'. It seems that there are no classes that implement `__sklearn_tags__` in the MRO and/or all classes in the MRO call `super().__sklearn_tags__()`. Make sure to inherit from `BaseEstimator` which implements `__sklearn_tags__` (or alternatively define `__sklearn_tags__` but we don't recommend this approach). Note that `BaseEstimator` needs to be on the right side of other Mixins in the inheritance order.

../../../miniconda3/envs/openoa-nrel/lib/python3.14/site-packages/sklearn/utils/_tags.py:283: AttributeError

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions