8000 Merge pull request #7216 from b0noI/master · scikit-learn/scikit-learn@db56cf4 · GitHub
[go: up one dir, main page]

Skip to content

Commit db56cf4

Browse files
authored
Merge pull request #7216 from b0noI/master
[MRG+1] New text for the ValueError that is thrown by _check_param_grid method
2 parents c931bf0 + f906b9f commit db56cf4

File tree

3 files changed

+26
-8
lines changed

3 files changed

+26
-8
lines changed

sklearn/grid_search.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -326,17 +326,18 @@ def _check_param_grid(param_grid):
326326
param_grid = [param_grid]
327327

328328
for p in param_grid:
329-
for v in p.values():
329+
for name, v in p.items():
330330
if isinstance(v, np.ndarray) and v.ndim > 1:
331331
raise ValueError("Parameter array should be one-dimensional.")
332332

333333
check = [isinstance(v, k) for k in (list, tuple, np.ndarray)]
334334
if True not in check:
335-
raise ValueError("Parameter values should be a list.")
335+
raise ValueError("Parameter values for parameter ({0}) need "
336+
"to be a sequence.".format(name))
336337

337338
if len(v) == 0:
338-
raise ValueError("Parameter values should be a non-empty "
339-
"list.")
339+
raise ValueError("Parameter values for parameter ({0}) need "
340+
"to be a non-empty sequence.".format(name))
340341

341342

342343
class _CVScoreTuple (namedtuple('_CVScoreTuple',

sklearn/model_selection/_search.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -328,17 +328,18 @@ def _check_param_grid(param_grid):
328328
param_grid = [param_grid]
329329

330330
for p in param_grid:
331-
for v in p.values():
331+
for name, v in p.items():
332332
if isinstance(v, np.ndarray) and v.ndim > 1:
333333
raise ValueError("Parameter array should be one-dimensional.")
334334

335335
check = [isinstance(v, k) for k in (list, tuple, np.ndarray)]
336336
if True not in check:
337-
raise ValueError("Parameter values should be a list.")
337+
raise ValueError("Parameter values for parameter ({0}) need "
338+
"to be a sequence.".format(name))
338339

339340
if len(v) == 0:
340-
raise ValueError("Parameter values should be a non-empty "
341-
"list.")
341+
raise ValueError("Parameter values for parameter ({0}) need "
342+
"to be a non-empty sequence.".format(name))
342343

343344

344345
# XXX Remove in 0.20

sklearn/model_selection/tests/test_search.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,22 @@ def test_grid_search():
168168
assert_raises(ValueError, grid_search.fit, X, y)
169169

170170

171+
def test_grid_search_incorrect_param_grid():
172+
clf = MockClassifier()
173+
assert_raise_message(
174+
ValueError,
175+
"Parameter values for parameter (C) need to be a sequence.",
176+
GridSearchCV, clf, {'C': 1})
177+
178+
179+
def test_grid_search_param_grid_includes_sequence_of_a_zero_length():
180+
clf = MockClassifier()
181+
assert_raise_message(
182+
ValueError,
183+
"Parameter values for parameter (C) need to be a non-empty sequence.",
184+
GridSearchCV, clf, {'C': []})
185+
186+
171187
@ignore_warnings
172188
def test_grid_search_no_score():
173189
# Test grid-search on classifier that has no score function.

0 commit comments

Comments
 (0)
0