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

Code before:
            make_regression(n_samples=10, random_state=0),
            ["predict_proba", "decision_function"],
        ),
        (
            KMeans(n_clusters=2, n_init=1),
            make_blobs(n_samples=10, random_state=0),
            "predict_proba",
        ),
        (
            KMeans(n_clusters=2, n_init=1),
            make_blobs(n_samples=10, random_state=0),
            ["predict_proba", "decision_function"],
        ),
        (
            IsolationForest(random_state=0),
            make_classification(n_samples=50, random_state=0),
            "predict_proba",
        ),
        (
            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,

Review comment (suggestion):
Can you please also add a test case for `DBSCAN()` with "predict"? (encountered here https://github.com/scikit-learn/scikit-learn/issues/33119#issuecomment-3811432790)

Code after:
def test_estimator_unsupported_response(pyplot, estimator, response_method):
    """Check the error message with not supported response method."""
    X, y = np.random.RandomState(0).randn(10, 2), np.array([0, 1] * 5)
    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(
    "estimator, response_method",
    [
        (LinearRegression(), "predict"),
        (KMeans(n_clusters=2, n_init=1), "predict"),
        (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"],