TypeError: got an unexpected keyword argument 'fit_params'
Describe the bug
I am running the latest example of mlxtend StackingCVClassifier and sklearn (GridSearchCV 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.
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.
I have fixed a similar issue some time ago #1109
Downstream nix package has this issue as well, see this pr which links to the pytest log
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
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.
@rasbt What do you think about updating the sklearn version in the unit test?
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.
I now patched @d-kleine 's fix directly into my lib files, as the fix is not yet merged
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