Skip to content

Commit

Permalink
Fixed _calc_score for *scikit-learn* version compatibility (#1109)
Browse files Browse the repository at this point in the history
* added sklearn version compability for fit_params

* added change to changelog

* fixed format

* fixed order of imports

* formatted with isort and black
  • Loading branch information
d-kleine authored Nov 5, 2024
1 parent a78bd0b commit 8e80778
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
2 changes: 2 additions & 0 deletions docs/sources/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ Files updated:
- ['mlxtend.frequent_patterns.fpcommon']
- ['mlxtend.frequent_patterns.fpgrowth'](https://rasbt.github.io/mlxtend/user_guide/frequent_patterns/fpgrowth/)
- ['mlxtend.frequent_patterns.fpmax'](https://rasbt.github.io/mlxtend/user_guide/frequent_patterns/fpmax/)
- ['mlxtend/feature_selection/utilities.py'](https://github.com/rasbt/mlxtend/blob/master/mlxtend/feature_selection/utilities.py)
- Modified `_calc_score` function to ensure compatibility with *scikit-learn* versions 1.4 and above by dynamically selecting between `fit_params` and `params` in `cross_val_score`.
- [`mlxtend.feature_selection.SequentialFeatureSelector`](https://github.com/rasbt/mlxtend/blob/master/mlxtend/feature_selection/sequential_feature_selector.py)
- Updated negative infinity constant to be compatible with old and new (>=2.0) `numpy` versions
- [`mlxtend.frequent_patterns.association_rules`](https://rasbt.github.io/mlxtend/user_guide/frequent_patterns/association_rules/)
Expand Down
8 changes: 5 additions & 3 deletions mlxtend/feature_selection/utilities.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from copy import deepcopy

import numpy as np
from sklearn import __version__ as sklearn_version
from sklearn.model_selection import cross_val_score


Expand Down Expand Up @@ -94,6 +93,9 @@ def _calc_score(
feature_groups = [[i] for i in range(X.shape[1])]

IDX = _merge_lists(feature_groups, indices)

param_name = "fit_params" if sklearn_version < "1.4" else "params"

if selector.cv:
scores = cross_val_score(
selector.est_,
Expand All @@ -104,7 +106,7 @@ def _calc_score(
scoring=selector.scorer,
n_jobs=1,
pre_dispatch=selector.pre_dispatch,
fit_params=fit_params,
**{param_name: fit_params},
)
else:
selector.est_.fit(X[:, IDX], y, **fit_params)
Expand Down

0 comments on commit 8e80778

Please sign in to comment.