Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

extreme_stable case for mean_tweedie_deviance #29258

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 4 commits into from
Jul 8, 2024
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
18 changes: 10 additions & 8 deletions 18 sklearn/metrics/_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -1301,12 +1301,14 @@ def _mean_tweedie_deviance(y_true, y_pred, sample_weight, power):
"""Mean Tweedie deviance regression loss."""
xp, _ = get_namespace(y_true, y_pred)
p = power
zero = xp.asarray(0, dtype=y_true.dtype)
if p < 0:
# 'Extreme stable', y any real number, y_pred > 0
dev = 2 * (
xp.pow(xp.where(y_true > 0, y_true, 0), 2 - p) / ((1 - p) * (2 - p))
- y_true * xp.pow(y_pred, 1 - p) / (1 - p)
+ xp.pow(y_pred, 2 - p) / (2 - p)
xp.pow(xp.where(y_true > 0, y_true, zero), xp.asarray(2 - p))
ogrisel marked this conversation as resolved.
Show resolved Hide resolved
/ ((1 - p) * (2 - p))
- y_true * xp.pow(y_pred, xp.asarray(1 - p)) / (1 - p)
+ xp.pow(y_pred, xp.asarray(2 - p)) / (2 - p)
)
elif p == 0:
# Normal distribution, y and y_pred any real number
Expand All @@ -1319,9 +1321,9 @@ def _mean_tweedie_deviance(y_true, y_pred, sample_weight, power):
dev = 2 * (xp.log(y_pred / y_true) + y_true / y_pred - 1)
else:
dev = 2 * (
xp.pow(y_true, 2 - p) / ((1 - p) * (2 - p))
- y_true * xp.pow(y_pred, 1 - p) / (1 - p)
+ xp.pow(y_pred, 2 - p) / (2 - p)
xp.pow(y_true, xp.asarray(2 - p)) / ((1 - p) * (2 - p))
- y_true * xp.pow(y_pred, xp.asarray(1 - p)) / (1 - p)
+ xp.pow(y_pred, xp.asarray(2 - p)) / (2 - p)
)
return float(_average(dev, weights=sample_weight))

Expand Down Expand Up @@ -1401,14 +1403,14 @@ def mean_tweedie_deviance(y_true, y_pred, *, sample_weight=None, power=0):
message = f"Mean Tweedie deviance error with power={power} can only be used on "
if power < 0:
# 'Extreme stable', y any real number, y_pred > 0
if (y_pred <= 0).any():
if xp.any(y_pred <= 0):
raise ValueError(message + "strictly positive y_pred.")
elif power == 0:
# Normal, y and y_pred can be any real number
pass
elif 1 <= power < 2:
# Poisson and compound Poisson distribution, y >= 0, y_pred > 0
if (y_true < 0).any() or (y_pred <= 0).any():
if xp.any(y_true < 0) or xp.any(y_pred <= 0):
raise ValueError(message + "non-negative y and strictly positive y_pred.")
elif power >= 2:
# Gamma and Extreme stable distribution, y and y_pred > 0
Expand Down
2 changes: 2 additions & 0 deletions 2 sklearn/metrics/tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -1942,6 +1942,8 @@ def check_array_api_metric_pairwise(metric, array_namespace, device, dtype_name)
check_array_api_multiclass_classification_metric,
],
mean_tweedie_deviance: [check_array_api_regression_metric],
partial(mean_tweedie_deviance, power=-0.5): [check_array_api_regression_metric],
ogrisel marked this conversation as resolved.
Show resolved Hide resolved
partial(mean_tweedie_deviance, power=1.5): [check_array_api_regression_metric],
r2_score: [
check_array_api_regression_metric,
check_array_api_regression_metric_multioutput,
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.