Repository: scikit-learn/scikit-learn
File: sklearn/metrics/tests/test_classification.py (Python)

Code before:
def test_likelihood_ratios_replace_undefined_by_0_tn(replace_undefined_by, expected):
    """Test that the `replace_undefined_by` param returns the right value for the
    negative_likelihood_ratio as defined by the user."""
    # This data causes tn=0 (0 true negatives) in the confusion_matrix and a division
    # by zero that affects the negative_likelihood_ratio:
    y_true = np.array([1, 0, 0])
    y_pred = np.array([1, 1, 1])

    _, negative_likelihood_ratio = class_likelihood_ratios(
        y_true, y_pred, replace_undefined_by=replace_undefined_by
    )

    if np.isnan(expected):
        assert np.isnan(negative_likelihood_ratio)
    else:
        assert negative_likelihood_ratio == pytest.approx(expected)


def test_cohen_kappa():
    # These label vectors reproduce the contingency matrix from Artstein and
    # Poesio (2008), Table 1: np.array([[20, 20], [10, 50]]).
    y1 = np.array([0] * 40 + [1] * 60)
    y2 = np.array([0] * 20 + [1] * 20 + [0] * 10 + [1] * 50)
    kappa = cohen_kappa_score(y1, y2)
    assert_almost_equal(kappa, 0.348, decimal=3)
    assert kappa == cohen_kappa_score(y2, y1)

    # Add spurious labels and ignore them.
    y1 = np.append(y1, [2] * 4)
    y2 = np.append(y2, [2] * 4)
    assert cohen_kappa_score(y1, y2, labels=[0, 1]) == kappa

    assert_almost_equal(cohen_kappa_score(y1, y1), 1.0)

    # Multiclass example: Artstein and Poesio, Table 4.
    y1 = np.array([0] * 46 + [1] * 44 + [2] * 10)
    y2 = np.array([0] * 52 + [1] * 32 + [2] * 16)
    assert_almost_equal(cohen_kappa_score(y1, y2), 0.8013, decimal=4)

    # Weighting example: none, linear, quadratic.
    y1 = np.array([0] * 46 + [1] * 44 + [2] * 10)
    y2 = np.array([0] * 50 + [1] * 40 + [2] * 10)
    assert_almost_equal(cohen_kappa_score(y1, y2), 0.9315, decimal=4)
    assert_almost_equal(cohen_kappa_score(y1, y2, weights="linear"), 0.9412, decimal=4)
    assert_almost_equal(
        cohen_kappa_score(y1, y2, weights="quadratic"), 0.9541, decimal=4
    )


@pytest.mark.parametrize(
    "test_case",

Review comment (suggestion):
```suggestion
@ignore_warnings(category=sklearn.exceptions.UndefinedMetricWarning)
@pytest.mark.parametrize(
```

Code after:
    assert kappa == cohen_kappa_score(y2, y1)

    # Add spurious labels and ignore them.
    y1 = np.append(y1, [2] * 4)
    y2 = np.append(y2, [2] * 4)
    assert cohen_kappa_score(y1, y2, labels=[0, 1]) == kappa

    assert_almost_equal(cohen_kappa_score(y1, y1), 1.0)

    # Multiclass example: Artstein and Poesio, Table 4.
    y1 = np.array([0] * 46 + [1] * 44 + [2] * 10)
    y2 = np.array([0] * 52 + [1] * 32 + [2] * 16)
    assert_almost_equal(cohen_kappa_score(y1, y2), 0.8013, decimal=4)

    # Weighting example: none, linear, quadratic.
    y1 = np.array([0] * 46 + [1] * 44 + [2] * 10)
    y2 = np.array([0] * 50 + [1] * 40 + [2] * 10)
    assert_almost_equal(cohen_kappa_score(y1, y2), 0.9315, decimal=4)
    assert_almost_equal(cohen_kappa_score(y1, y2, weights="linear"), 0.9412, decimal=4)
    assert_almost_equal(
        cohen_kappa_score(y1, y2, weights="quadratic"), 0.9541, decimal=4
    )


@ignore_warnings(category=UndefinedMetricWarning)
@pytest.mark.parametrize(
    "test_case",
    [
        # annotator y2 does not assign any label specified in `labels` (note: also
        # applicable if `labels` is default and `y2` does not contain any label that is
        # in `y1`):
        ([1] * 5 + [2] * 5, [3] * 10, [1, 2], None),
        # both inputs (`y1` and `y2`) only have one label:
        ([3] * 10, [3] * 10, None, None),
        # both inputs only have one label in common that is also in `labels`:
        ([1] * 5 + [2] * 5, [1] * 5 + [3] * 5, [1, 2], None),
        # like the last test case, but with `weights="linear"` (note that
        # weights="linear" and weights="quadratic" are different branches, though the
        # latter is so similar to the former that the test case is skipped here):
        ([1] * 5 + [2] * 5, [1] * 5 + [3] * 5, [1, 2], "linear"),
    ],
)
@pytest.mark.parametrize("replace_undefined_by", [0.0, np.nan])
def test_cohen_kappa_undefined(test_case, replace_undefined_by):
    """Test that cohen_kappa_score handles divisions by 0 correctly by returning the
    `replace_undefined_by` param. (The first test case covers the first possible
    location in the function for an occurrence of a division by zero, the last three
    test cases cover a zero division in the the second possible location in the
    function."""

    y1, y2, labels, weights = test_case