mlxtend icon indicating copy to clipboard operation
mlxtend copied to clipboard

TypeError: got an unexpected keyword argument 'fit_params'

Open Whisht opened this issue 1 year ago • 8 comments

Describe the bug

I am running the latest example of mlxtend StackingCVClassifier and sklearnGridSearchCV StackingCVClassifier: Stacking with cross-validation - mlxtend).

If works normally with sklearn 1-3.2, but failed with sklearn 1.6.0 in the error TypeError: got an unexpected keyword argument 'fit_params'. I believe that it because sklearn has deprecated the fit_params since version 1.4.

Steps/Code to Reproduce

from sklearn.linear_model import LogisticRegression
from sklearn.neighbors import KNeighborsClassifier
from sklearn.naive_bayes import GaussianNB 
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import GridSearchCV
from mlxtend.classifier import StackingCVClassifier

# Initializing models

clf1 = KNeighborsClassifier(n_neighbors=1)
clf2 = RandomForestClassifier(random_state=RANDOM_SEED)
clf3 = GaussianNB()
lr = LogisticRegression()

sclf = StackingCVClassifier(classifiers=[clf1, clf2, clf3], 
                            meta_classifier=lr,
                            random_state=42)

params = {'kneighborsclassifier__n_neighbors': [1, 5],
          'randomforestclassifier__n_estimators': [10, 50],
          'meta_classifier__C': [0.1, 10.0]}

grid = GridSearchCV(estimator=sclf, 
                    param_grid=params, 
                    cv=5,
                    refit=True)
grid.fit(X, y)

cv_keys = ('mean_test_score', 'std_test_score', 'params')

for r, _ in enumerate(grid.cv_results_['mean_test_score']):
    print("%0.3f +/- %0.2f %r"
          % (grid.cv_results_[cv_keys[0]][r],
             grid.cv_results_[cv_keys[1]][r] / 2.0,
             grid.cv_results_[cv_keys[2]][r]))

print('Best parameters: %s' % grid.best_params_)
print('Accuracy: %.2f' % grid.best_score_)

I tried to modify the code of mlxtend/classifier/stacking_cv_classification.py", line 269 from

            prediction = cross_val_predict(
                model,
                X,
                y,
                groups=groups,
                cv=final_cv,
                n_jobs=self.n_jobs,
                fit_params=fit_params,
                verbose=self.verbose,
                pre_dispatch=self.pre_dispatch,
                method="predict_proba" if self.use_probas else "predict",
            )

to

            prediction = cross_val_predict(
                model,
                X,
                y,
                groups=groups,
                cv=final_cv,
                n_jobs=self.n_jobs,
                params=fit_params, # change this line to params
                verbose=self.verbose,
                pre_dispatch=self.pre_dispatch,
                method="predict_proba" if self.use_probas else "predict",
            )

.

The code is working normally.

Whisht avatar Dec 11 '24 07:12 Whisht

I have the same issue with scikit-learn 1.6.2

downgrading to 1.4.2 solves the issue but instead i get a lot of warnings:

[...]\sklearn\model_selection\_validation.py:73: FutureWarning: fit_paramsis deprecated and will be removed in version 1.6. Pass parameters viaparams instead.

jimmy927 avatar Jan 16 '25 00:01 jimmy927

I have fixed a similar issue some time ago #1109

d-kleine avatar Jan 20 '25 22:01 d-kleine

Downstream nix package has this issue as well, see this pr which links to the pytest log

philipwilk avatar Mar 23 '25 15:03 philipwilk

Relevant upstream commits/docs: changelog commit

philipwilk avatar Mar 23 '25 15:03 philipwilk

Potential patch which does it the same way @d-kleine did in #1109

Related test failures are resolved, but there are now some failing asserts failure log

philipwilk avatar Mar 23 '25 16:03 philipwilk

Your github unit tests have scikit-learn pinned to 1.3.1, which is from sep21 2023, which I think is why they are not catching these float64 issues.

philipwilk avatar Mar 23 '25 17:03 philipwilk

@rasbt What do you think about updating the sklearn version in the unit test?

d-kleine avatar Apr 06 '25 14:04 d-kleine

Oh yeah that would be a good idea. I think it might require some other adjustments in other places then as well. It is on my backlog but not sure when I get around to it. Thanks for suggesting.

rasbt avatar Apr 06 '25 22:04 rasbt

I now patched @d-kleine 's fix directly into my lib files, as the fix is not yet merged

Arminius4 avatar Jun 19 '25 00:06 Arminius4

I completely missed this earlier. Many thanks for the fix @d-kleine ! @Arminius4 , after the recent merge, this should now also work via the main branch:

pip install git+git://github.com/rasbt/mlxtend.git#egg=mlxtend

rasbt avatar Jun 19 '25 23:06 rasbt