Repository: scikit-learn/scikit-learn
File: sklearn/preprocessing/_label.py (Python)

Code before:
            "Multioutput target data is not supported with label binarization"
        )
    if y_type == "unknown":
        raise ValueError("The type of target data is not known")

    xp, _, device_ = get_namespace_and_device(y)

    try:
        classes = xp.asarray(classes, device=device_)

    # e.g., torch.tensor(["yes", "no"])
    # or cupy.asarray(["yes", "no"])
    except (ValueError, TypeError) as e:
        y = _convert_to_numpy(y, xp=xp)
        xp, _, device_ = get_namespace_and_device(y)
        classes = xp.asarray(classes)

    if sparse_output and not _is_numpy_namespace(xp):
        raise ValueError(
            "`sparse_output=True` is not supported for Array API "
            "namespace {xp.__name__}. "
            "Use `sparse_output=False` to return a dense array instead."
        )

    n_samples = y.shape[0] if hasattr(y, "shape") else len(y)
    n_classes = classes.shape[0] if hasattr(classes, "shape") else len(classes)
    dtype_ = _find_matching_floating_dtype(y, xp=xp)
    int_dtype_ = (
        y.dtype if hasattr(y, "dtype") and "int" in str(y.dtype) else indexing_dtype(xp)
    )

    if y_type == "binary":
        if n_classes == 1:
            if sparse_output:
                return sp.csr_matrix((n_samples, 1), dtype=int)
            else:
                Y = xp.zeros((n_samples, 1), dtype=dtype_)
                Y += neg_label
                return Y
        elif n_classes >= 3:
            y_type = "multiclass"

    sorted_class = xp.sort(classes)
    if y_type == "multilabel-indicator":
        y_n_classes = y.shape[1] if hasattr(y, "shape") else len(y[0])
        if classes.size != y_n_classes:
            raise ValueError(
                "classes {0} mismatch with the labels {1} found in the data".format(
                    classes, unique_labels(y)
                )
            )

Review comment (suggestion):
I think `classes` will be an `xp array` (`numpy` in the except case)
```suggestion
    n_classes = classes.shape[0]
```

Code after:

    y_type = type_of_target(y)
    if "multioutput" in y_type:
        raise ValueError(
            "Multioutput target data is not supported with label binarization"
        )
    if y_type == "unknown":
        raise ValueError("The type of target data is not known")

    xp, is_array_api, device_ = get_namespace_and_device(y)

    if is_array_api and sparse_output and not _is_numpy_namespace(xp):
        raise ValueError(
            "`sparse_output=True` is not supported for array API "
            f"'namespace {xp.__name__}'. "
            "Use `sparse_output=False` to return a dense array instead."
        )

    try:
        classes = xp.asarray(classes, device=device_)
    except (ValueError, TypeError) as e:
        # `classes` contains an unsupported dtype for this namespace.
        # For example, attempting to create torch.tensor(["yes", "no"]) will fail.
        raise ValueError(
            f"`classes` contains unsupported dtype for array API namespace "
            f"'{xp.__name__}'."
        ) from e

    n_samples = y.shape[0] if hasattr(y, "shape") else len(y)
    n_classes = classes.shape[0]
    if hasattr(y, "dtype") and xp.isdtype(y.dtype, "integral"):
        int_dtype_ = y.dtype
    else:
        int_dtype_ = indexing_dtype(xp)

    if y_type == "binary":
        if n_classes == 1:
            if sparse_output:
                return sp.csr_matrix((n_samples, 1), dtype=int)
            else:
                Y = xp.zeros((n_samples, 1), dtype=int_dtype_)
                Y += neg_label
                return Y
        elif n_classes >= 3:
            y_type = "multiclass"

    sorted_class = xp.sort(classes)
    if y_type == "multilabel-indicator":
        y_n_classes = y.shape[1] if hasattr(y, "shape") else len(y[0])
        if n_classes != y_n_classes:
            raise ValueError(