Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a70232d

Browse files
thomasjpfanglemaitre
authored andcommitted
BUG Fixes verbose > 2 for grid search (#19659)
1 parent c299b80 commit a70232d

File tree

3 files changed

+40
-6
lines changed

3 files changed

+40
-6
lines changed

doc/whats_new/v0.24.rst

Lines 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

Lines 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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2117,3 +2117,22 @@ def test_search_cv_using_minimal_compatible_estimator(SearchCV, Predictor):
21172117
else:
21182118
assert_allclose(y_pred, y.mean())
21192119
assert search.score(X, y) == pytest.approx(r2_score(y, y_pred))
2120+
2121+
2122+
@pytest.mark.parametrize("return_train_score", [True, False])
2123+
def test_search_cv_verbose_3(capsys, return_train_score):
2124+
"""Check that search cv with verbose>2 shows the score for single
2125+
metrics. non-regression test fo #19658."""
2126+
X, y = make_classification(n_samples=100, n_classes=2, flip_y=.2,
2127+
random_state=0)
2128+
clf = LinearSVC(random_state=0)
2129+
grid = {'C': [.1]}
2130+
2131+
GridSearchCV(clf, grid, scoring='accuracy', verbose=3, cv=3,
2132+
return_train_score=return_train_score).fit(X, y)
2133+
captured = capsys.readouterr().out
2134+
if return_train_score:
2135+
match = re.findall(r"score=\(train=[\d\.]+, test=[\d.]+\)", captured)
2136+
else:
2137+
match = re.findall(r"score=[\d\.]+", captured)
2138+
assert len(match) == 3

0 commit comments

Comments
 (0)