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

add array API support for d2_tweedie_score #29207

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
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
1 change: 1 addition & 0 deletions 1 doc/modules/array_api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ Metrics
-------

- :func:`sklearn.metrics.accuracy_score`
- :func:`sklearn.metrics.d2_tweedie_score`
- :func:`sklearn.metrics.mean_absolute_error`
- :func:`sklearn.metrics.mean_tweedie_deviance`
- :func:`sklearn.metrics.pairwise.cosine_similarity`
Expand Down
1 change: 1 addition & 0 deletions 1 doc/whats_new/v1.6.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ See :ref:`array_api` for more details.

**Functions:**

- :func:`sklearn.metrics.d2_tweedie_score` :pr:`29207` by :user:`Emily Chen <EmilyXinyi>`;
- :func:`sklearn.metrics.mean_absolute_error` :pr:`27736` by :user:`Edoardo Abati <EdAbati>`;
- :func:`sklearn.metrics.mean_squared_error` :pr:`29142` by :user:`Yaroslav Korobko <Tialo>`;
- :func:`sklearn.metrics.mean_tweedie_deviance` :pr:`28106` by :user:`Thomas Li <lithomas1>`;
Expand Down
8 changes: 5 additions & 3 deletions 8 sklearn/metrics/_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -1598,8 +1598,10 @@ def d2_tweedie_score(y_true, y_pred, *, sample_weight=None, power=0):
>>> d2_tweedie_score(y_true, y_true, power=2)
1.0
"""
xp, _ = get_namespace(y_true, y_pred)

y_type, y_true, y_pred, _ = _check_reg_targets(
y_true, y_pred, None, dtype=[np.float64, np.float32]
y_true, y_pred, None, dtype=[xp.float64, xp.float32], xp=xp
)
if y_type == "continuous-multioutput":
raise ValueError("Multioutput not supported in d2_tweedie_score")
Expand All @@ -1609,12 +1611,12 @@ def d2_tweedie_score(y_true, y_pred, *, sample_weight=None, power=0):
warnings.warn(msg, UndefinedMetricWarning)
return float("nan")

y_true, y_pred = np.squeeze(y_true), np.squeeze(y_pred)
y_true, y_pred = xp.squeeze(y_true, axis=1), xp.squeeze(y_pred, axis=1)
numerator = mean_tweedie_deviance(
y_true, y_pred, sample_weight=sample_weight, power=power
)

y_avg = np.average(y_true, weights=sample_weight)
y_avg = _average(y_true, weights=sample_weight, xp=xp)
denominator = _mean_tweedie_deviance(
y_true, y_avg, sample_weight=sample_weight, power=power
)
Expand Down
3 changes: 3 additions & 0 deletions 3 sklearn/metrics/tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -1942,6 +1942,9 @@ def check_array_api_metric_pairwise(metric, array_namespace, device, dtype_name)
check_array_api_regression_metric,
check_array_api_multioutput_regression_metric,
],
d2_tweedie_score: [
check_array_api_regression_metric,
],
}


Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.