Open
Description
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.