Skip to content

BUG Fixes verbose > 2 for grid search #19659

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions doc/whats_new/v0.24.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ Changelog
sample_weight object is not modified anymore. :pr:`19182` by
:user:`Yosuke KOBAYASHI <m7142yosuke>`.

:mod:`sklearn.model_selection`
..............................

- |Fix| :class:`model_selection.RandomizedSearchCV` and
:class:`model_selection.GridSearchCV` now correctly shows the score for
single metrics and verbose > 2. :pr:`19659` by `Thomas Fan`_.

:mod:`sklearn.preprocessing`
............................

Expand Down
20 changes: 14 additions & 6 deletions sklearn/model_selection/_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -631,13 +631,21 @@ def _fit_and_score(estimator, X, y, scorer, train, test, verbose,
total_time = score_time + fit_time
end_msg = f"[CV{progress_msg}] END "
result_msg = params_msg + (";" if params_msg else "")
if verbose > 2 and isinstance(test_scores, dict):
for scorer_name in sorted(test_scores):
result_msg += f" {scorer_name}: ("
if verbose > 2:
if isinstance(test_scores, dict):
for scorer_name in sorted(test_scores):
result_msg += f" {scorer_name}: ("
if return_train_score:
scorer_scores = train_scores[scorer_name]
result_msg += f"train={scorer_scores:.3f}, "
result_msg += f"test={test_scores[scorer_name]:.3f})"
else:
result_msg += ", score="
if return_train_score:
scorer_scores = train_scores[scorer_name]
result_msg += f"train={scorer_scores:.3f}, "
result_msg += f"test={test_scores[scorer_name]:.3f})"
result_msg += (f"(train={train_scores:.3f}, "
f"test={test_scores:.3f})")
else:
result_msg += f"{test_scores:.3f}"
result_msg += f" total time={logger.short_format_time(total_time)}"

# Right align the result_msg
Expand Down
19 changes: 19 additions & 0 deletions sklearn/model_selection/tests/test_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -2137,3 +2137,22 @@ def test_search_cv_using_minimal_compatible_estimator(SearchCV, Predictor):
else:
assert_allclose(y_pred, y.mean())
assert search.score(X, y) == pytest.approx(r2_score(y, y_pred))


@pytest.mark.parametrize("return_train_score", [True, False])
def test_search_cv_verbose_3(capsys, return_train_score):
"""Check that search cv with verbose>2 shows the score for single
metrics. non-regression test fo #19658."""
X, y = make_classification(n_samples=100, n_classes=2, flip_y=.2,
random_state=0)
clf = LinearSVC(random_state=0)
grid = {'C': [.1]}

GridSearchCV(clf, grid, scoring='accuracy', verbose=3, cv=3,
return_train_score=return_train_score).fit(X, y)
captured = capsys.readouterr().out
if return_train_score:
match = re.findall(r"score=\(train=[\d\.]+, test=[\d.]+\)", captured)
else:
match = re.findall(r"score=[\d\.]+", captured)
assert len(match) == 3