Skip to content

MNT Add more sample weight checks in regression metric common tests #31726

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 1 commit into from
Jul 11, 2025
Merged
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
13 changes: 13 additions & 0 deletions sklearn/metrics/tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -1614,6 +1614,19 @@ def test_regression_with_invalid_sample_weight(name):
with pytest.raises(ValueError, match="Found input variables with inconsistent"):
metric(y_true, y_pred, sample_weight=sample_weight)

sample_weight = random_state.random_sample(size=(n_samples,))
sample_weight[0] = np.inf
with pytest.raises(ValueError, match="Input sample_weight contains infinity"):
metric(y_true, y_pred, sample_weight=sample_weight)

sample_weight[0] = np.nan
with pytest.raises(ValueError, match="Input sample_weight contains NaN"):
metric(y_true, y_pred, sample_weight=sample_weight)

sample_weight = np.array([1 + 2j, 3 + 4j, 5 + 7j])
with pytest.raises(ValueError, match="Complex data not supported"):
metric(y_true[:3], y_pred[:3], sample_weight=sample_weight)

sample_weight = random_state.random_sample(size=(n_samples * 2,)).reshape(
(n_samples, 2)
)
Expand Down