Skip to content

Commit 8816591

Browse files
authored
Make common test_check_targets_array_api (scikit-learn#34487)
1 parent 6099369 commit 8816591

3 files changed

Lines changed: 44 additions & 37 deletions

File tree

sklearn/metrics/_classification.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,10 @@ def _check_targets(y_true, y_pred, sample_weight=None, xp=None, device=None):
124124
"""
125125
if xp is None or device is None:
126126
xp, _, device = get_namespace_and_device(y_pred, xp=xp)
127-
# Ensure `y_true` and `sample_weight` are on the `y_pred` device and namespace.
128-
y_true, sample_weight = move_to(y_true, sample_weight, xp=xp, device=device)
127+
# Move to specified namespace and device, or the namespace and device of `y_pred`
128+
y_true, y_pred, sample_weight = move_to(
129+
y_true, y_pred, sample_weight, xp=xp, device=device
130+
)
129131
y_true, y_pred = attach_unique(y_true, y_pred)
130132

131133
check_consistent_length(y_true, y_pred, sample_weight)

sklearn/metrics/tests/test_common.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@
6262
zero_one_loss,
6363
)
6464
from sklearn.metrics._base import _average_binary_score
65+
from sklearn.metrics._classification import _check_targets
66+
from sklearn.metrics._regression import _check_reg_targets
6567
from sklearn.metrics.pairwise import (
6668
additive_chi2_kernel,
6769
chi2_kernel,
@@ -2049,6 +2051,44 @@ def test_metrics_pos_label_error_str(metric, y_pred_threshold, dtype_y_str):
20492051
metric(y1, y2)
20502052

20512053

2054+
@pytest.mark.parametrize("check", (_check_targets, _check_reg_targets))
2055+
def test_check_targets_array_api(check):
2056+
"""Ensure `check` returns arrays in the correct namespace and device."""
2057+
xp, _ = _array_api_for_tests("array_api_strict")
2058+
y_pred = np.asarray([[0, 0, 0, 1], [1, 0, 1, 1], [0, 0, 0, 1]])
2059+
y_pred_xp = xp.asarray(y_pred)
2060+
2061+
kwarg = (
2062+
{"multioutput": np.asarray([0.2, 0.2, 0.25, 0.35])}
2063+
if check is _check_reg_targets
2064+
else {}
2065+
)
2066+
with config_context(array_api_dispatch=True):
2067+
# Do not pass `xp` and `device`
2068+
outputs = check(
2069+
y_true=np.asarray([[1, 0, 0, 1], [0, 1, 1, 1], [1, 1, 0, 1]]),
2070+
y_pred=y_pred_xp,
2071+
sample_weight=np.asarray([1, 1, 2]),
2072+
**kwarg,
2073+
)
2074+
for output in outputs[1:]:
2075+
assert get_namespace(output)[0] == xp
2076+
assert array_api_device(output) == array_api_device(y_pred_xp)
2077+
2078+
# Pass `xp` and `device`
2079+
outputs = check(
2080+
y_true=np.asarray([[1, 0, 0, 1], [0, 1, 1, 1], [1, 1, 0, 1]]),
2081+
y_pred=y_pred,
2082+
sample_weight=np.asarray([1, 1, 2]),
2083+
xp=xp,
2084+
device=array_api_device(y_pred_xp),
2085+
**kwarg,
2086+
)
2087+
for output in outputs[1:]:
2088+
assert get_namespace(output)[0] == xp
2089+
assert array_api_device(output) == array_api_device(y_pred_xp)
2090+
2091+
20522092
def check_array_api_metric(
20532093
metric, array_namespace, device_name, dtype_name, a_np, b_np, **metric_kwargs
20542094
):

sklearn/metrics/tests/test_regression.py

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
from scipy import optimize
77
from scipy.special import factorial, xlogy
88

9-
from sklearn._config import config_context
109
from sklearn.dummy import DummyRegressor
1110
from sklearn.exceptions import UndefinedMetricWarning
1211
from sklearn.metrics import (
@@ -29,9 +28,7 @@
2928
)
3029
from sklearn.metrics._regression import _check_reg_targets
3130
from sklearn.model_selection import GridSearchCV
32-
from sklearn.utils._array_api import device, get_namespace
3331
from sklearn.utils._testing import (
34-
_array_api_for_tests,
3532
assert_almost_equal,
3633
assert_array_almost_equal,
3734
assert_array_equal,
@@ -367,38 +364,6 @@ def test__check_reg_targets_single_output_error():
367364
_check_reg_targets([1, 2, 3, 4], [1.5, 2.5, 3.5, 4.5], None, multioutput=[0.5])
368365

369366

370-
def test__check_reg_targets_array_api():
371-
"""Check arrays are returned in the correct namespace."""
372-
xp, _ = _array_api_for_tests("array_api_strict")
373-
y_pred = np.asarray([[0, 0, 0, 1], [1, 0, 1, 1], [0, 0, 0, 1]])
374-
y_pred_xp = xp.asarray(y_pred)
375-
376-
with config_context(array_api_dispatch=True):
377-
# Do not pass `xp` and `device`
378-
outputs = _check_reg_targets(
379-
y_true=np.asarray([[1, 0, 0, 1], [0, 1, 1, 1], [1, 1, 0, 1]]),
380-
y_pred=y_pred_xp,
381-
sample_weight=np.asarray([1, 1, 2]),
382-
multioutput=np.asarray([0.2, 0.2, 0.25, 0.35]),
383-
)
384-
for output in outputs[1:]:
385-
assert get_namespace(output)[0] == xp
386-
assert device(output) == device(y_pred_xp)
387-
388-
# Pass `xp` and `device`
389-
outputs = _check_reg_targets(
390-
y_true=np.asarray([[1, 0, 0, 1], [0, 1, 1, 1], [1, 1, 0, 1]]),
391-
y_pred=y_pred,
392-
sample_weight=np.asarray([1, 1, 2]),
393-
multioutput=np.asarray([0.2, 0.2, 0.25, 0.35]),
394-
xp=xp,
395-
device=device(y_pred_xp),
396-
)
397-
for output in outputs[1:]:
398-
assert get_namespace(output)[0] == xp
399-
assert device(output) == device(y_pred_xp)
400-
401-
402367
def test_regression_multioutput_array():
403368
y_true = [[1, 2], [2.5, -1], [4.5, 3], [5, 7]]
404369
y_pred = [[1, 1], [2, -1], [5, 4], [5, 6.5]]

0 commit comments

Comments
 (0)