Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 6f180d7

Browse filesBrowse files
authored
BUG Fixes verbose > 2 for grid search (#19659)
1 parent 5980455 commit 6f180d7
Copy full SHA for 6f180d7

File tree

3 files changed

+40
-6
lines changed
Filter options

3 files changed

+40
-6
lines changed

‎doc/whats_new/v0.24.rst

Copy file name to clipboardExpand all lines: doc/whats_new/v0.24.rst
+7Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ Changelog
3333
sample_weight object is not modified anymore. :pr:`19182` by
3434
:user:`Yosuke KOBAYASHI <m7142yosuke>`.
3535

36+
:mod:`sklearn.model_selection`
37+
..............................
38+
39+
- |Fix| :class:`model_selection.RandomizedSearchCV` and
40+
:class:`model_selection.GridSearchCV` now correctly shows the score for
41+
single metrics and verbose > 2. :pr:`19659` by `Thomas Fan`_.
42+
3643
:mod:`sklearn.preprocessing`
3744
............................
3845

‎sklearn/model_selection/_validation.py

Copy file name to clipboardExpand all lines: sklearn/model_selection/_validation.py
+14-6Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -631,13 +631,21 @@ def _fit_and_score(estimator, X, y, scorer, train, test, verbose,
631631
total_time = score_time + fit_time
632632
end_msg = f"[CV{progress_msg}] END "
633633
result_msg = params_msg + (";" if params_msg else "")
634-
if verbose > 2 and isinstance(test_scores, dict):
635-
for scorer_name in sorted(test_scores):
636-
result_msg += f" {scorer_name}: ("
634+
if verbose > 2:
635+
if isinstance(test_scores, dict):
636+
for scorer_name in sorted(test_scores):
637+
result_msg += f" {scorer_name}: ("
638+
if return_train_score:
639+
scorer_scores = train_scores[scorer_name]
640+
result_msg += f"train={scorer_scores:.3f}, "
641+
result_msg += f"test={test_scores[scorer_name]:.3f})"
642+
else:
643+
result_msg += ", score="
637644
if return_train_score:
638-
scorer_scores = train_scores[scorer_name]
639-
result_msg += f"train={scorer_scores:.3f}, "
640-
result_msg += f"test={test_scores[scorer_name]:.3f})"
645+
result_msg += (f"(train={train_scores:.3f}, "
646+
f"test={test_scores:.3f})")
647+
else:
648+
result_msg += f"{test_scores:.3f}"
641649
result_msg += f" total time={logger.short_format_time(total_time)}"
642650

643651
# Right align the result_msg

‎sklearn/model_selection/tests/test_search.py

Copy file name to clipboardExpand all lines: sklearn/model_selection/tests/test_search.py
+19Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2137,3 +2137,22 @@ def test_search_cv_using_minimal_compatible_estimator(SearchCV, Predictor):
21372137
else:
21382138
assert_allclose(y_pred, y.mean())
21392139
assert search.score(X, y) == pytest.approx(r2_score(y, y_pred))
2140+
2141+
2142+
@pytest.mark.parametrize("return_train_score", [True, False])
2143+
def test_search_cv_verbose_3(capsys, return_train_score):
2144+
"""Check that search cv with verbose>2 shows the score for single
2145+
metrics. non-regression test fo #19658."""
2146+
X, y = make_classification(n_samples=100, n_classes=2, flip_y=.2,
2147+
random_state=0)
2148+
clf = LinearSVC(random_state=0)
2149+
grid = {'C': [.1]}
2150+
2151+
GridSearchCV(clf, grid, scoring='accuracy', verbose=3, cv=3,
2152+
return_train_score=return_train_score).fit(X, y)
2153+
captured = capsys.readouterr().out
2154+
if return_train_score:
2155+
match = re.findall(r"score=\(train=[\d\.]+, test=[\d.]+\)", captured)
2156+
else:
2157+
match = re.findall(r"score=[\d\.]+", captured)
2158+
assert len(match) == 3

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.