Skip to content

Sklearn API Changes #278

Description

@janwendt

Describe the bug

TypeError: RandomizedSearchCV.fit got unexpected argument(s) {'groups'}, which are not routed to any object.
because of sklearn API changes in January 2024 (1.4.0). Since then there is no groups parameter for the fit function anymore. Same error applies to GridSearchCV.

Wondering if anybody still uses probatus cause sklearn is now at 1.7.1 so the error should occour since ~20 months? Looks like an amazing module especially with early stopping!!

Or did the error got introduced with this PR? #275

UPDATE: Downgrading to 3.1.2 solves the problem so bug got introduced with linked PR

Environment (please complete the following information):

  • probatus version 3.1.3
  • python version 3.12.9
  • OS: windows

To Reproduce

from sklearn.model_selection import RandomizedSearchCV
from probatus.feature_elimination import ShapRFECV
from xgboost import XGBRegressor
from sklearn.datasets import make_regression

# generate data
X, y = make_regression(n_samples=1000, n_features=20, noise=0.1, random_state=42)

model = XGBRegressor(n_estimators=10000)

param_grid = {
    "learning_rate": [0.01, 0.05, 0.1, 0.2],
    "max_depth": [3, 4, 5, 6, 7, 8, 9, 10]
}

search = RandomizedSearchCV(model, param_grid)

shap_elimination = ShapRFECV(
    model=search, step=0.2, cv=5, scoring="roc_auc", eval_metric="auc", early_stopping_rounds=10, n_jobs=-1
)
report = shap_elimination.fit_compute(X, y)

Error traceback

If applicable please provide full traceback of the error.

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[467], line 20
     16 # Run feature elimination
     17 shap_elimination = ShapRFECV(
     18     model=search, step=0.2, cv=5, scoring="roc_auc", eval_metric="auc", early_stopping_rounds=10, n_jobs=-1
     19 )
---> 20 report = shap_elimination.fit_compute(X, y_target)
     23 #  eval_set=[(X_test, y_test)]
     24 # Make plots
     25 performance_plot = shap_elimination.plot()

File ~\AppData\Local\anaconda3\envs\jw\Lib\site-packages\probatus\feature_elimination\feature_elimination.py:312, in ShapRFECV.fit_compute(self, X, y, sample_weight, columns_to_keep, column_names, groups, shap_variance_penalty_factor, **shap_kwargs)
    248 def fit_compute(
    249     self,
    250     X,
   (...)    257     **shap_kwargs,
    258 ):
    259     """
    260     Fits the object with the provided data.
    261 
   (...)    309             DataFrame containing results of feature elimination from each iteration.
    310     """
--> 312     self.fit(
    313         X,
    314         y,
    315         sample_weight=sample_weight,
    316         columns_to_keep=columns_to_keep,
    317         column_names=column_names,
    318         groups=groups,
    319         shap_variance_penalty_factor=shap_variance_penalty_factor,
    320         **shap_kwargs,
    321     )
    322     return self.compute()

File ~\AppData\Local\anaconda3\envs\jw\Lib\site-packages\probatus\feature_elimination\feature_elimination.py:457, in ShapRFECV.fit(self, X, y, sample_weight, columns_to_keep, column_names, groups, shap_variance_penalty_factor, **shap_kwargs)
    455 # Optimize parameters
    456 if self.search_model:
--> 457     current_search_model = clone(self.model).fit(X=current_X, y=self.y, groups=groups)
    458     current_model = current_search_model.estimator.set_params(**current_search_model.best_params_)
    459 else:

File ~\AppData\Local\anaconda3\envs\jw\Lib\site-packages\sklearn\base.py:1389, in _fit_context.<locals>.decorator.<locals>.wrapper(estimator, *args, **kwargs)
   1382     estimator._validate_params()
   1384 with config_context(
   1385     skip_parameter_validation=(
   1386         prefer_skip_nested_validation or global_skip_validation
   1387     )
   1388 ):
-> 1389     return fit_method(estimator, *args, **kwargs)

File ~\AppData\Local\anaconda3\envs\jw\Lib\site-packages\sklearn\model_selection\_search.py:931, in BaseSearchCV.fit(self, X, y, **params)
    928 X, y = indexable(X, y)
    929 params = _check_method_params(X, params=params)
--> 931 routed_params = self._get_routed_params_for_fit(params)
    933 cv_orig = check_cv(self.cv, y, classifier=is_classifier(estimator))
    934 n_splits = cv_orig.get_n_splits(X, y, **routed_params.splitter.split)

File ~\AppData\Local\anaconda3\envs\jw\Lib\site-packages\sklearn\model_selection\_search.py:876, in BaseSearchCV._get_routed_params_for_fit(self, params)
    870 """Get the parameters to be used for routing.
    871 
    872 This is a method instead of a snippet in ``fit`` since it's used twice,
    873 here in ``fit``, and in ``HalvingRandomSearchCV.fit``.
    874 """
    875 if _routing_enabled():
--> 876     routed_params = process_routing(self, "fit", **params)
    877 else:
    878     params = params.copy()

File ~\AppData\Local\anaconda3\envs\jw\Lib\site-packages\sklearn\utils\_metadata_requests.py:1590, in process_routing(_obj, _method, **kwargs)
   1584     raise TypeError(
   1585         f"Can only route and process input on these methods: {METHODS}, "
   1586         f"while the passed method is: {_method}."
   1587     )
   1589 request_routing = get_routing_for_object(_obj)
-> 1590 request_routing.validate_metadata(params=kwargs, method=_method)
   1591 routed_params = request_routing.route_params(params=kwargs, caller=_method)
   1593 return routed_params

File ~\AppData\Local\anaconda3\envs\jw\Lib\site-packages\sklearn\utils\_metadata_requests.py:1073, in MetadataRouter.validate_metadata(self, method, params)
   1071 extra_keys = set(params.keys()) - param_names - self_params
   1072 if extra_keys:
-> 1073     raise TypeError(
   1074         f"{self.owner}.{method} got unexpected argument(s) {extra_keys}, which"
   1075         " are not routed to any object."
   1076     )

TypeError: RandomizedSearchCV.fit got unexpected argument(s) {'groups'}, which are not routed to any object.

Expected behavior

No error.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions