Skip to content
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

FIX methods in model_selection/_validation accept params=None with metadata routing enabled #30451

Merged
merged 6 commits into from
Jan 2, 2025
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- :func:`~model_selection.cross_validate`, :func:`~model_selection.cross_val_predict`,
and :func:`~model_selection.cross_val_score` now accept `params=None` when metadata
routing is enabled. By `Adrin Jalali`_
3 changes: 2 additions & 1 deletion sklearn/model_selection/_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ def cross_validate(
_check_groups_routing_disabled(groups)

X, y = indexable(X, y)

params = {} if params is None else params
cv = check_cv(cv, y, classifier=is_classifier(estimator))

scorers = check_scoring(
Expand Down Expand Up @@ -1172,6 +1172,7 @@ def cross_val_predict(
"""
_check_groups_routing_disabled(groups)
X, y = indexable(X, y)
params = {} if params is None else params

if _routing_enabled():
# For estimators, a MetadataRouter is created in get_metadata_routing
Expand Down
21 changes: 21 additions & 0 deletions sklearn/model_selection/tests/test_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2539,6 +2539,27 @@ def test_groups_with_routing_validation(func, extra_args):
)


@pytest.mark.parametrize(
"func, extra_args",
[
(cross_validate, {}),
(cross_val_score, {}),
(cross_val_predict, {}),
(learning_curve, {}),
(permutation_test_score, {}),
(validation_curve, {"param_name": "alpha", "param_range": np.array([1])}),
],
)
@config_context(enable_metadata_routing=True)
def test_cross_validate_params_none(func, extra_args):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a docstring for the test? It would be difficult to understand what it's testing later otherwise.

Or maybe, we can join this test with the next one (test_passed_unrequested_metadata) and modify the docstring there.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect, thank you. :)

"""Test that no errors are raised when passing `params=None`, which is the
default value.
Non-regression test for: https://github.com/scikit-learn/scikit-learn/issues/30447
"""
X, y = make_classification(n_samples=100, n_classes=2, random_state=0)
func(estimator=ConsumingClassifier(), X=X, y=y, **extra_args)


@pytest.mark.parametrize(
"func, extra_args",
[
Expand Down
Loading