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

Code before:
    pos_label = 2
    neg_label = 0
    expected = pos_label * y_ind
    y = arr_type(y_ind)

    check_binarized_results(y, classes, pos_label, neg_label, expected)

    with pytest.raises(ValueError):
        label_binarize(
            y, classes=classes, neg_label=-1, pos_label=pos_label, sparse_output=True
        )


def test_invalid_input_label_binarize():
    with pytest.raises(ValueError):
        label_binarize([0, 2], classes=[0, 2], pos_label=0, neg_label=1)
    with pytest.raises(ValueError, match="continuous target data is not "):
        label_binarize([1.2, 2.7], classes=[0, 1])
    with pytest.raises(ValueError, match="mismatch with the labels"):
        label_binarize([[1, 3]], classes=[1, 2, 3])


@pytest.mark.parametrize(
    "string_labels, y, classes, expected",
    [
        [True, [1, 0, 0, 1], ["yes", "no"], [[0], [0], [0], [0]]],
        [
            True,
            [1, 0, 2, 9],
            ["bird", "cat", "dog"],
            [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]],
        ],
        [False, [1, 0, 0, 1], [0, 1], [[1], [0], [0], [1]]],
        [False, [1, 0, 2, 9], [0, 1, 2], [[0, 1, 0], [1, 0, 0], [0, 0, 1], [0, 0, 0]]],
    ],
)
@pytest.mark.parametrize(
    "array_namespace, device_, dtype_name", yield_namespace_device_dtype_combinations()
)
def test_label_binarize_array_api_compliance(
    string_labels, y, classes, expected, array_namespace, device_, dtype_name
):
    """Test that :func:`label_binarize` works correctly with the Array API for binary
    and multi-class inputs for numerical labels and non-sparse outputs.
    """
    xp = _array_api_for_tests(array_namespace, device_)

    with config_context(array_api_dispatch=True):
        y = xp.asarray(y, device=device_)

        # `sparse_output=True` is not allowed for non-NumPy namespace

Review comment (bug):
I don't understand in which context one would expect integer valued `y` and `str` valued `classes`. To me, if `classes` holds `str` values, so would `y`, and it would be fine to raise a `ValueError` if that's not the case.

It seems like a bug not to do so.

If we fix this bug, then, adding array API support should become cleaner: there would be no need to catch the `cupy.asarray(["yes", "no"])` exceptions because they can never be raised.

Code after:
    + LIL_CONTAINERS,
)
def test_label_binarize_multilabel(arr_type):
    y_ind = np.array([[0, 1, 0], [1, 1, 1], [0, 0, 0]])
    classes = [0, 1, 2]
    pos_label = 2
    neg_label = 0
    expected = pos_label * y_ind
    y = arr_type(y_ind)

    check_binarized_results(y, classes, pos_label, neg_label, expected)

    with pytest.raises(ValueError):
        label_binarize(
            y, classes=classes, neg_label=-1, pos_label=pos_label, sparse_output=True
        )


def test_invalid_input_label_binarize():
    with pytest.raises(ValueError):
        label_binarize([0, 2], classes=[0, 2], pos_label=0, neg_label=1)
    with pytest.raises(ValueError, match="continuous target data is not "):
        label_binarize([1.2, 2.7], classes=[0, 1])
    with pytest.raises(ValueError, match="mismatch with the labels"):
        label_binarize([[1, 3]], classes=[1, 2, 3])


@pytest.mark.parametrize(
    "y, classes, expected",
    [
        [[1, 0, 0, 1], ["yes", "no"], [[0], [0], [0], [0]]],
        [
            [1, 0, 2, 9],
            ["bird", "cat", "dog"],
            [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]],
        ],
        [[1, 0, 0, 1], [0, 1], [[1], [0], [0], [1]]],
        [[1, 0, 2, 1], [0, 1, 2], [[0, 1, 0], [1, 0, 0], [0, 0, 1], [0, 1, 0]]],
    ],
)
@pytest.mark.parametrize(
    "array_namespace, device_, dtype_name", yield_namespace_device_dtype_combinations()
)
def test_label_binarize_array_api_compliance(
    y, classes, expected, array_namespace, device_, dtype_name
):
    """Test that :func:`label_binarize` works correctly with the Array API for binary
    and multi-class inputs for numerical labels and non-sparse outputs.
    """
    xp = _array_api_for_tests(array_namespace, device_)
    xp_is_numpy = _is_numpy_namespace(xp)