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

Code before:
            "Sparse binarization is only supported with non "
            "zero pos_label and zero neg_label, got "
            "pos_label={0} and neg_label={1}"
            "".format(pos_label, neg_label)
        )

    # To account for pos_label == 0 in the dense case
    pos_switch = pos_label == 0
    if pos_switch:
        pos_label = -neg_label

    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, _, 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_)

Review comment (suggestion):
I don't think these api's support string arrays. So maybe this comment either needs to be modified or removed. What case are we trying to handle here? So basically if `y` and therefore `classes` is an array of strings we can be confident we are not dealing with the array API.

Code after:
        )

    if sparse_output and (pos_label == 0 or neg_label != 0):
        raise ValueError(
            "Sparse binarization is only supported with non "
            "zero pos_label and zero neg_label, got "
            "pos_label={0} and neg_label={1}"
            "".format(pos_label, neg_label)
        )

    # To account for pos_label == 0 in the dense case
    pos_switch = pos_label == 0
    if pos_switch:
        pos_label = -neg_label

    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: