Skip to content

ENH Add Array Api compatibility for mean_squared_error #29142

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
Jun 6, 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
5 changes: 2 additions & 3 deletions doc/whats_new/v1.6.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,9 @@ See :ref:`array_api` for more details.

**Functions:**

- :func:`sklearn.metrics.mean_tweedie_deviance` now supports Array API compatible
inputs.
:pr:`28106` by :user:`Thomas Li <lithomas1>`;
- :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>`;
- :func:`sklearn.metrics.pairwise.cosine_similarity` :pr:`29014` by :user:`Edoardo Abati <EdAbati>`.


Expand Down
14 changes: 10 additions & 4 deletions sklearn/metrics/_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ def mean_squared_error(

Returns
-------
loss : float or ndarray of floats
loss : float or array of floats
A non-negative floating point value (the best value is 0.0), or an
array of floating point values, one for each individual target.

Expand Down Expand Up @@ -519,11 +519,14 @@ def mean_squared_error(
y_true, y_pred, sample_weight=sample_weight, multioutput=multioutput
)

xp, _ = get_namespace(y_true, y_pred, sample_weight, multioutput)
dtype = _find_matching_floating_dtype(y_true, y_pred, xp=xp)

y_type, y_true, y_pred, multioutput = _check_reg_targets(
y_true, y_pred, multioutput
y_true, y_pred, multioutput, dtype=dtype, xp=xp
)
check_consistent_length(y_true, y_pred, sample_weight)
output_errors = np.average((y_true - y_pred) ** 2, axis=0, weights=sample_weight)
output_errors = _average((y_true - y_pred) ** 2, axis=0, weights=sample_weight)

if isinstance(multioutput, str):
if multioutput == "raw_values":
Expand All @@ -532,7 +535,10 @@ def mean_squared_error(
# pass None as weights to np.average: uniform mean
multioutput = None

return np.average(output_errors, weights=multioutput)
# See comment in mean_absolute_error
mean_squared_error = _average(output_errors, weights=multioutput)
assert mean_squared_error.shape == ()
return float(mean_squared_error)


@validate_params(
Expand Down
4 changes: 4 additions & 0 deletions sklearn/metrics/tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -1938,6 +1938,10 @@ def check_array_api_metric_pairwise(metric, array_namespace, device, dtype_name)
check_array_api_regression_metric,
check_array_api_multioutput_regression_metric,
],
mean_squared_error: [
check_array_api_regression_metric,
check_array_api_multioutput_regression_metric,
],
}


Expand Down
Loading