Repository: scikit-learn/scikit-learn
File: sklearn/utils/tests/test_response.py (Python)

Code before:
        (
            IsolationForest(random_state=0),
            make_classification(n_samples=50, random_state=0),
            ["predict_proba", "score"],
        ),
    ],
)
def test_estimator_unsupported_response(pyplot, estimator, Xy, response_method):
    """Check the error message with not supported response method."""
    X, y = Xy[0], Xy[1]
    estimator.fit(X, y)
    err_msg = "has none of the following attributes:"
    with pytest.raises(AttributeError, match=err_msg):
        _get_response_values(
            estimator,
            X,
            response_method=response_method,
        )


@pytest.mark.parametrize(
    "response_method",
    ["predict", ["predict", "not-a-valid-method"]],
)
@pytest.mark.parametrize("return_response_method_used", [True, False])
def test_get_response_values_regressor(response_method, return_response_method_used):
    """Check the behaviour of `_get_response_values` with regressor."""
    X, y = make_regression(n_samples=10, random_state=0)
    regressor = LinearRegression().fit(X, y)
    results = _get_response_values(
        regressor,
        X,
        response_method=response_method,
        return_response_method_used=return_response_method_used,
    )
    chosen_response_method = (
        response_method[0] if isinstance(response_method, list) else response_method
    )
    prediction_method = getattr(regressor, chosen_response_method)
    assert_array_equal(results[0], prediction_method(X))
    assert results[1] is None
    if return_response_method_used:
        assert results[2] == chosen_response_method


@pytest.mark.parametrize(
    "response_method",
    ["predict", "score", ["predict", "score"], ["predict", "not-a-valid-method"]],
)
@pytest.mark.parametrize("return_response_method_used", [True, False])
def test_get_response_values_clusterer(response_method, return_response_method_used):

Review comment (refactor):
There is a lot of duplicate code in `test_get_response_values_regressor/outlier_detection/clusterer`. Can you try to parametrize the estimators here as well
(similar to `test_response_values_output_shape_`)?

Code after:
        (KMeans(n_clusters=2, n_init=1), "score"),
        (KMeans(n_clusters=2, n_init=1), ["predict", "score"]),
        (IsolationForest(random_state=0), "predict"),
        (IsolationForest(random_state=0), "decision_function"),
        (IsolationForest(random_state=0), ["decision_function", "predict"]),
    ],
)
@pytest.mark.parametrize("return_response_method_used", [True, False])
def test_estimator_get_response_values(
    estimator, response_method, return_response_method_used
):
    """Check the behaviour of `_get_response_values`."""
    X, y = np.random.RandomState(0).randn(10, 2), np.array([0, 1] * 5)
    estimator.fit(X, y)
    results = _get_response_values(
        estimator,
        X,
        response_method=response_method,
        return_response_method_used=return_response_method_used,
    )
    chosen_response_method = (
        response_method[0] if isinstance(response_method, list) else response_method
    )
    prediction_method = getattr(estimator, chosen_response_method)
    assert_array_equal(results[0], prediction_method(X))
    assert results[1] is None
    if return_response_method_used:
        assert results[2] == chosen_response_method


@pytest.mark.parametrize(
    "response_method",
    ["predict_proba", "decision_function", "predict", "predict_log_proba"],
)
def test_get_response_values_classifier_unknown_pos_label(response_method):
    """Check that `_get_response_values` raises the proper error message with
    classifier."""
    X, y = make_classification(n_samples=10, n_classes=2, random_state=0)
    classifier = LogisticRegression().fit(X, y)

    # provide a `pos_label` which is not in `y`
    err_msg = r"pos_label=whatever is not a valid label: It should be one of \[0 1\]"
    with pytest.raises(ValueError, match=err_msg):
        _get_response_values(
            classifier,
            X,
            response_method=response_method,
            pos_label="whatever",
        )