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

Code before:


def test_average_precision_score_multiclass_pos_label_errors():
    # Raise an error for multiclass y_true with pos_label other than 1
    y_true = np.array([0, 1, 2, 0, 1, 2])
    y_score = np.array(
        [
            [0.5, 0.2, 0.1],
            [0.4, 0.5, 0.3],
            [0.1, 0.2, 0.6],
            [0.2, 0.3, 0.5],
            [0.2, 0.3, 0.5],
            [0.2, 0.3, 0.5],
        ]
    )
    err_msg = (
        "Parameter pos_label is fixed to 1 for multiclass y_true. "
        "Do not set pos_label or set pos_label to 1."
    )
    with pytest.raises(ValueError, match=err_msg):
        average_precision_score(y_true, y_score, pos_label=3)


def test_multiclass_ranking_metrics_raise_for_incorrect_shape_of_y_score():
    """Test the ranking metrics that support multiclass values in `y_true`
    raise if shape `y_score` is 1D"""
    y_true = np.array([0, 1, 2, 0, 1, 2])
    y_score = np.array([0.5, 0.4, 0.8, 0.9, 0.8, 0.7])

    msg = re.escape("`y_score` needs to be of shape `(n_samples, n_classes)`")
    with pytest.raises(ValueError, match=msg):
        average_precision_score(y_true, y_score)
    with pytest.raises(ValueError, match=msg):
        roc_auc_score(y_true, y_score, multi_class="ovr")
    with pytest.raises(ValueError, match=msg):
        top_k_accuracy_score(y_true, y_score)


def test_score_scale_invariance():
    # Test that average_precision_score and roc_auc_score are invariant by
    # the scaling or shifting of probabilities
    # This test was expanded (added scaled_down) in response to github
    # issue #3864 (and others), where overly aggressive rounding was causing
    # problems for users with very small y_score values
    y_true, _, y_score = make_prediction(binary=True)

    roc_auc = roc_auc_score(y_true, y_score)
    roc_auc_scaled_up = roc_auc_score(y_true, 100 * y_score)
    roc_auc_scaled_down = roc_auc_score(y_true, 1e-6 * y_score)
    roc_auc_shifted = roc_auc_score(y_true, y_score - 10)
    assert roc_auc == roc_auc_scaled_up

Review comment (suggestion):
```suggestion
    """Test ranking metrics, with multiclass support, raises if shape `y_score` is 1D"""
```
nit, to make it fit on one line, as per docstring standard

Code after:


def test_average_precision_score_multiclass_pos_label_errors():
    # Raise an error for multiclass y_true with pos_label other than 1
    y_true = np.array([0, 1, 2, 0, 1, 2])
    y_score = np.array(
        [
            [0.5, 0.2, 0.1],
            [0.4, 0.5, 0.3],
            [0.1, 0.2, 0.6],
            [0.2, 0.3, 0.5],
            [0.2, 0.3, 0.5],
            [0.2, 0.3, 0.5],
        ]
    )
    err_msg = (
        "Parameter pos_label is fixed to 1 for multiclass y_true. "
        "Do not set pos_label or set pos_label to 1."
    )
    with pytest.raises(ValueError, match=err_msg):
        average_precision_score(y_true, y_score, pos_label=3)


def test_multiclass_ranking_metrics_raise_for_incorrect_shape_of_y_score():
    """Test ranking metrics, with multiclass support, raise if shape `y_score` is 1D."""
    y_true = np.array([0, 1, 2, 0, 1, 2])
    y_score = np.array([0.5, 0.4, 0.8, 0.9, 0.8, 0.7])

    msg = re.escape("`y_score` needs to be of shape `(n_samples, n_classes)`")
    with pytest.raises(ValueError, match=msg):
        average_precision_score(y_true, y_score)
    with pytest.raises(ValueError, match=msg):
        roc_auc_score(y_true, y_score, multi_class="ovr")
    with pytest.raises(ValueError, match=msg):
        top_k_accuracy_score(y_true, y_score)


def test_score_scale_invariance():
    # Test that average_precision_score and roc_auc_score are invariant by
    # the scaling or shifting of probabilities
    # This test was expanded (added scaled_down) in response to github
    # issue #3864 (and others), where overly aggressive rounding was causing
    # problems for users with very small y_score values
    y_true, _, y_score = make_prediction(binary=True)

    roc_auc = roc_auc_score(y_true, y_score)
    roc_auc_scaled_up = roc_auc_score(y_true, 100 * y_score)
    roc_auc_scaled_down = roc_auc_score(y_true, 1e-6 * y_score)
    roc_auc_shifted = roc_auc_score(y_true, y_score - 10)
    assert roc_auc == roc_auc_scaled_up
    assert roc_auc == roc_auc_scaled_down