| 1 | diff --git a/sklearn/utils/multiclass.py b/sklearn/utils/multiclass.py |
| 2 | --- a/sklearn/utils/multiclass.py |
| 3 | +++ b/sklearn/utils/multiclass.py |
| 4 | @@ -155,14 +155,25 @@ def is_multilabel(y): |
| 5 | if hasattr(y, "__array__") or isinstance(y, Sequence) or is_array_api: |
| 6 | # DeprecationWarning will be replaced by ValueError, see NEP 34 |
| 7 | # https://numpy.org/neps/nep-0034-infer-dtype-is-object.html |
| 8 | + check_y_kwargs = dict( |
| 9 | + accept_sparse=True, |
| 10 | + allow_nd=True, |
| 11 | + force_all_finite=False, |
| 12 | + ensure_2d=False, |
| 13 | + ensure_min_samples=0, |
| 14 | + ensure_min_features=0, |
| 15 | + ) |
| 16 | with warnings.catch_warnings(): |
| 17 | warnings.simplefilter("error", np.VisibleDeprecationWarning) |
| 18 | try: |
| 19 | - y = xp.asarray(y) |
| 20 | - except (np.VisibleDeprecationWarning, ValueError): |
| 21 | + y = check_array(y, dtype=None, **check_y_kwargs) |
| 22 | + except (np.VisibleDeprecationWarning, ValueError) as e: |
| 23 | + if str(e).startswith("Complex data not supported"): |
| 24 | + raise |
| 25 | + |
| 26 | # dtype=object should be provided explicitly for ragged arrays, |
| 27 | # see NEP 34 |
| 28 | - y = xp.asarray(y, dtype=object) |
| 29 | + y = check_array(y, dtype=object, **check_y_kwargs) |
| 30 | |
| 31 | if not (hasattr(y, "shape") and y.ndim == 2 and y.shape[1] > 1): |
| 32 | return False |
| 33 | @@ -302,15 +313,27 @@ def type_of_target(y, input_name=""): |
| 34 | # https://numpy.org/neps/nep-0034-infer-dtype-is-object.html |
| 35 | # We therefore catch both deprecation (NumPy < 1.24) warning and |
| 36 | # value error (NumPy >= 1.24). |
| 37 | + check_y_kwargs = dict( |
| 38 | + accept_sparse=True, |
| 39 | + allow_nd=True, |
| 40 | + force_all_finite=False, |
| 41 | + ensure_2d=False, |
| 42 | + ensure_min_samples=0, |
| 43 | + ensure_min_features=0, |
| 44 | + ) |
| 45 | + |
| 46 | with warnings.catch_warnings(): |
| 47 | warnings.simplefilter("error", np.VisibleDeprecationWarning) |
| 48 | if not issparse(y): |
| 49 | try: |
| 50 | - y = xp.asarray(y) |
| 51 | - except (np.VisibleDeprecationWarning, ValueError): |
| 52 | + y = check_array(y, dtype=None, **check_y_kwargs) |
| 53 | + except (np.VisibleDeprecationWarning, ValueError) as e: |
| 54 | + if str(e).startswith("Complex data not supported"): |
| 55 | + raise |
| 56 | + |
| 57 | # dtype=object should be provided explicitly for ragged arrays, |
| 58 | # see NEP 34 |
| 59 | - y = xp.asarray(y, dtype=object) |
| 60 | + y = check_array(y, dtype=object, **check_y_kwargs) |
| 61 | |
| 62 | # The old sequence of sequences format |
| 63 | try: |