Skip to content

MAINT Parameter validation for sklearn.metrics.d2_pinball_score #25414

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 9 commits into from
Feb 7, 2023
22 changes: 14 additions & 8 deletions sklearn/metrics/_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -1304,6 +1304,18 @@ def d2_tweedie_score(y_true, y_pred, *, sample_weight=None, power=0):
return 1 - numerator / denominator


@validate_params(
{
"y_true": ["array-like"],
"y_pred": ["array-like"],
"sample_weight": ["array-like", None],
"alpha": [Interval(Real, 0, 1, closed="both")],
"multioutput": [
StrOptions({"raw_values", "uniform_average"}),
"array-like",
],
}
)
def d2_pinball_score(
y_true, y_pred, *, sample_weight=None, alpha=0.5, multioutput="uniform_average"
):
Expand All @@ -1327,7 +1339,7 @@ def d2_pinball_score(
y_pred : array-like of shape (n_samples,) or (n_samples, n_outputs)
Estimated target values.

sample_weight : array-like of shape (n_samples,), optional
sample_weight : array-like of shape (n_samples,), default=None
Sample weights.

alpha : float, default=0.5
Expand Down Expand Up @@ -1434,15 +1446,9 @@ def d2_pinball_score(
if multioutput == "raw_values":
# return scores individually
return output_scores
elif multioutput == "uniform_average":
else: # multioutput == "uniform_average"
# passing None as weights to np.average results in uniform mean
avg_weights = None
else:
raise ValueError(
"multioutput is expected to be 'raw_values' "
"or 'uniform_average' but we got %r"
" instead." % multioutput
)
else:
avg_weights = multioutput

Expand Down
3 changes: 0 additions & 3 deletions sklearn/metrics/tests/test_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,9 +351,6 @@ def test_regression_multioutput_array():
with pytest.raises(ValueError, match=err_msg):
mean_pinball_loss(y_true, y_pred, multioutput="variance_weighted")

with pytest.raises(ValueError, match=err_msg):
d2_pinball_score(y_true, y_pred, multioutput="variance_weighted")

pbl = mean_pinball_loss(y_true, y_pred, multioutput="raw_values")
mape = mean_absolute_percentage_error(y_true, y_pred, multioutput="raw_values")
r = r2_score(y_true, y_pred, multioutput="raw_values")
Expand Down
1 change: 1 addition & 0 deletions sklearn/tests/test_public_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ def _check_function_param_validation(
"sklearn.metrics.cluster.contingency_matrix",
"sklearn.metrics.cohen_kappa_score",
"sklearn.metrics.confusion_matrix",
"sklearn.metrics.d2_pinball_score",
"sklearn.metrics.det_curve",
"sklearn.metrics.hamming_loss",
"sklearn.metrics.mean_absolute_error",
Expand Down