Skip to content

FIX _check_reg_targets with Array API and multioutput argument #29143

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 20, 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
3 changes: 2 additions & 1 deletion doc/whats_new/v1.6.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ See :ref:`array_api` for more details.
- :func:`sklearn.metrics.cluster.entropy` :pr:`29141` by :user:`Yaroslav Korobko <Tialo>`;
- :func:`sklearn.metrics.d2_tweedie_score` :pr:`29207` by :user:`Emily Chen <EmilyXinyi>`;
- :func:`sklearn.metrics.max_error` :pr:`29212` by :user:`Edoardo Abati <EdAbati>`;
- :func:`sklearn.metrics.mean_absolute_error` :pr:`27736` by :user:`Edoardo Abati <EdAbati>`;
- :func:`sklearn.metrics.mean_absolute_error` :pr:`27736` by :user:`Edoardo Abati <EdAbati>`
and :pr:`29143` by :user:`Tialo <Tialo>` and :user:`Loïc Estève <lesteve>`;
- :func:`sklearn.metrics.mean_gamma_deviance` :pr:`29239` by :usser:`Emily Chen <EmilyXinyi>`;
- :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
6 changes: 3 additions & 3 deletions sklearn/metrics/_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,10 @@ def _check_reg_targets(y_true, y_pred, multioutput, dtype="numeric", xp=None):
multioutput = check_array(multioutput, ensure_2d=False)
if n_outputs == 1:
raise ValueError("Custom weights are useful only in multi-output cases.")
elif n_outputs != len(multioutput):
elif n_outputs != multioutput.shape[0]:
raise ValueError(
"There must be equally many custom weights (%d) as outputs (%d)."
% (len(multioutput), n_outputs)
"There must be equally many custom weights "
f"({multioutput.shape[0]}) as outputs ({n_outputs})."
)
y_type = "continuous" if n_outputs == 1 else "continuous-multioutput"

Expand Down
35 changes: 26 additions & 9 deletions sklearn/metrics/tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -1762,6 +1762,10 @@ def check_array_api_metric(
metric_kwargs["sample_weight"], device=device
)

multioutput = metric_kwargs.get("multioutput")
if isinstance(multioutput, np.ndarray):
metric_kwargs["multioutput"] = xp.asarray(multioutput, device=device)

with config_context(array_api_dispatch=True):
metric_xp = metric(a_xp, b_xp, **metric_kwargs)

Expand Down Expand Up @@ -1869,8 +1873,8 @@ def check_array_api_regression_metric(metric, array_namespace, device, dtype_nam
def check_array_api_regression_metric_multioutput(
metric, array_namespace, device, dtype_name
):
y_true_np = np.array([[1, 3], [1, 2]], dtype=dtype_name)
y_pred_np = np.array([[1, 4], [1, 1]], dtype=dtype_name)
y_true_np = np.array([[1, 3, 2], [1, 2, 2]], dtype=dtype_name)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added more values to avoid having n_rows == n_columns

y_pred_np = np.array([[1, 4, 4], [1, 1, 1]], dtype=dtype_name)

check_array_api_metric(
metric,
Expand All @@ -1894,12 +1898,25 @@ def check_array_api_regression_metric_multioutput(
sample_weight=sample_weight,
)

check_array_api_metric(
metric,
array_namespace,
device,
dtype_name,
a_np=y_true_np,
b_np=y_pred_np,
multioutput=np.array([0.1, 0.3, 0.7], dtype=dtype_name),
)

def check_array_api_multioutput_regression_metric(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before, there was check_array_api_multioutput_regression_metric (multioutput in the middle) and check_array_api_regression_metric_multioutput (multioutput at the end), I merged both functions into one.

metric, array_namespace, device, dtype_name
):
metric = partial(metric, multioutput="raw_values")
check_array_api_regression_metric(metric, array_namespace, device, dtype_name)
check_array_api_metric(
metric,
array_namespace,
device,
dtype_name,
a_np=y_true_np,
b_np=y_pred_np,
multioutput="raw_values",
)


def check_array_api_metric_pairwise(metric, array_namespace, device, dtype_name):
Expand Down Expand Up @@ -1949,11 +1966,11 @@ def check_array_api_metric_pairwise(metric, array_namespace, device, dtype_name)
cosine_similarity: [check_array_api_metric_pairwise],
mean_absolute_error: [
check_array_api_regression_metric,
check_array_api_multioutput_regression_metric,
check_array_api_regression_metric_multioutput,
],
mean_squared_error: [
check_array_api_regression_metric,
check_array_api_multioutput_regression_metric,
check_array_api_regression_metric_multioutput,
],
d2_tweedie_score: [
check_array_api_regression_metric,
Expand Down