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

Skip to content

Commit 872052b

Browse files
FIX convert cv_results_ values to numpy array in SuccessiveHalving (#19211)
Co-authored-by: Thomas J. Fan <[email protected]>
1 parent 7fa2e6e commit 872052b

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed

doc/whats_new/v0.24.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ Changelog
4040
:class:`model_selection.GridSearchCV` now correctly shows the score for
4141
single metrics and verbose > 2. :pr:`19659` by `Thomas Fan`_.
4242

43+
- |Fix| Some values in the `cv_results_` attribute of
44+
:class:`model_selection.HalvingRandomSearchCV` and
45+
:class:`model_selection.HalvingGridSearchCV` were not properly converted to
46+
numpy arrays. :pr:`19211` by `Nicolas Hug`_.
47+
4348
:mod:`sklearn.preprocessing`
4449
............................
4550

sklearn/model_selection/_search.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -897,6 +897,10 @@ def _format_results(self, candidate_params, n_splits, out,
897897
out = _aggregate_score_dicts(out)
898898

899899
results = dict(more_results or {})
900+
for key, val in results.items():
901+
# each value is a list (as per evaluate_candidate's convention)
902+
# we convert it to an array for consistency with the other keys
903+
results[key] = np.asarray(val)
900904

901905
def _store(key_name, array, weights=None, splits=False, rank=False):
902906
"""A small helper to store the scores/times to the cv_results_"""

sklearn/model_selection/tests/test_successive_halving.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,12 @@ def scorer(est, X, y):
445445
sh.set_params(n_candidates=2 * 30, min_resources='exhaust')
446446

447447
sh.fit(X, y)
448+
449+
# non-regression check for
450+
# https://github.com/scikit-learn/scikit-learn/issues/19203
451+
assert isinstance(sh.cv_results_['iter'], np.ndarray)
452+
assert isinstance(sh.cv_results_['n_resources'], np.ndarray)
453+
448454
cv_results_df = pd.DataFrame(sh.cv_results_)
449455

450456
# just make sure we don't have ties

0 commit comments

Comments
 (0)