Skip to content

Fix __repr__ for RepeatedKFold and RepeatedStratifiedKFold #67

@rowan-stein

Description

@rowan-stein

User request

RepeatedKFold and RepeatedStratifiedKFold do not show correct repr string.

Description
Both classes currently inherit from _RepeatedSplits which lacks a repr. As a result, repr(...) returns the default object representation. Expected is a parameterized string, e.g.:

  • RepeatedKFold(n_splits=5, n_repeats=10, random_state=None)
  • RepeatedStratifiedKFold(n_splits=5, n_repeats=10, random_state=None)

Steps/Code to Reproduce

from sklearn.model_selection import RepeatedKFold, RepeatedStratifiedKFold
repr(RepeatedKFold())
repr(RepeatedStratifiedKFold())

Expected Results

RepeatedKFold(n_splits=5, n_repeats=10, random_state=None)
RepeatedStratifiedKFold(n_splits=5, n_repeats=10, random_state=None)

Actual Results

'<sklearn.model_selection._split.RepeatedKFold object at 0x...>'
'<sklearn.model_selection._split.RepeatedStratifiedKFold object at 0x...>'

Specification (from research)

  • File: sklearn/model_selection/_split.py
  • Import helper: from ..utils._pprint import _build_repr
  • Add repr in _RepeatedSplits:
def __repr__(self):
    return _build_repr(self)
  • Regression tests (sklearn/model_selection/tests/test_repeated_split_repr.py):
    • Assert default repr strings for RepeatedKFold and RepeatedStratifiedKFold using with config_context(print_changed_only=False)
    • Assert non-default parameter combinations also render correctly

Notes

  • Defaults assumed: n_splits=5, n_repeats=10, random_state=None
  • One PR will implement this change and add tests, targeting branch scikit-learn__scikit-learn-14983.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions