Skip to content

Commit

Permalink
Skip _check_param_grid import for scikit-learn > 1.0.2 (#901)
Browse files Browse the repository at this point in the history
* Only import _check_param_grid for sklearn <= 1.0.2 (test-upstream)

* Call _get_param_iterator in bad param grid tests (test-upstream)

* Resolve different errors raised in > 1.0.2 (test-upstream)
  • Loading branch information
charlesbluca authored Jan 20, 2022
1 parent 5466bec commit 1e811ce
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
12 changes: 9 additions & 3 deletions dask_ml/model_selection/_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
is_classifier,
)
from sklearn.exceptions import NotFittedError
from sklearn.model_selection._search import BaseSearchCV, _check_param_grid
from sklearn.model_selection._search import BaseSearchCV
from sklearn.model_selection._split import (
BaseShuffleSplit,
KFold,
Expand Down Expand Up @@ -71,6 +71,12 @@

__all__ = ["GridSearchCV", "RandomizedSearchCV"]

# scikit-learn > 1.0.2 removed _check_param_grid
if SK_VERSION <= packaging.version.parse("1.0.2"):
from sklearn.model_selection._search import _check_param_grid
else:
_check_param_grid = None

if SK_VERSION <= packaging.version.parse("0.21.dev0"):

_RETURN_TRAIN_SCORE_DEFAULT = "warn"
Expand Down Expand Up @@ -1600,8 +1606,8 @@ def __init__(
n_jobs=n_jobs,
cache_cv=cache_cv,
)

_check_param_grid(param_grid)
if _check_param_grid:
_check_param_grid(param_grid)
self.param_grid = param_grid

def _get_param_iterator(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -371,28 +371,34 @@ def test_grid_search_one_grid_point():


def test_grid_search_bad_param_grid():
# passing a non-iterable param grid raises a TypeError in scikit-learn > 1.0.2
iterable_err = (
ValueError if SK_VERSION <= packaging.version.parse("1.0.2") else TypeError
)

param_dict = {"C": 1.0}
clf = SVC()

with pytest.raises(ValueError):
dcv.GridSearchCV(clf, param_dict)
with pytest.raises(iterable_err):
dcv.GridSearchCV(clf, param_dict)._get_param_iterator()

param_dict = {"C": []}
clf = SVC()

with pytest.raises(ValueError):
dcv.GridSearchCV(clf, param_dict)
dcv.GridSearchCV(clf, param_dict)._get_param_iterator()

param_dict = {"C": "1,2,3"}
clf = SVC()

with pytest.raises(ValueError):
dcv.GridSearchCV(clf, param_dict)
with pytest.raises(iterable_err):
dcv.GridSearchCV(clf, param_dict)._get_param_iterator()

param_dict = {"C": np.ones(6).reshape(3, 2)}
clf = SVC()

with pytest.raises(ValueError):
dcv.GridSearchCV(clf, param_dict)
dcv.GridSearchCV(clf, param_dict)._get_param_iterator()


def test_grid_search_sparse():
Expand Down

0 comments on commit 1e811ce

Please sign in to comment.