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

Code before:
import pytest

from sklearn.base import clone
from sklearn.cluster import KMeans
from sklearn.datasets import (
    load_iris,
    make_blobs,
    make_classification,
    make_multilabel_classification,
    make_regression,
)
from sklearn.ensemble import IsolationForest
from sklearn.linear_model import LinearRegression, LogisticRegression
from sklearn.multioutput import ClassifierChain
from sklearn.preprocessing import scale
from sklearn.tree import DecisionTreeClassifier, DecisionTreeRegressor
from sklearn.utils._response import _get_response_values, _get_response_values_binary
from sklearn.utils._testing import assert_allclose, assert_array_equal

X, y = load_iris(return_X_y=True)
# scale the data to avoid ConvergenceWarning with LogisticRegression
X = scale(X, copy=False)
X_binary, y_binary = X[:100], y[:100]


@pytest.mark.parametrize("response_method", ["predict"])
@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,
    )
    prediction_method = getattr(regressor, response_method)
    assert_array_equal(results[0], prediction_method(X))
    assert results[1] is None
    if return_response_method_used:
        assert results[2] == response_method


@pytest.mark.parametrize(
    "response_method",
    ["predict", "score", ["predict", "score"]],
)
@pytest.mark.parametrize("return_response_method_used", [True, False])
def test_get_response_values_clusterer(response_method, return_response_method_used):
    """Check the behaviour of `_get_response_values` with clusterer."""

Review comment (suggestion):
Same comment as [below](https://github.com/scikit-learn/scikit-learn/pull/33126/changes#r2731684885)

Code after:
import pytest

from sklearn.base import clone
from sklearn.cluster import DBSCAN, KMeans
from sklearn.datasets import (
    load_iris,
    make_classification,
    make_multilabel_classification,
)
from sklearn.ensemble import IsolationForest
from sklearn.linear_model import LinearRegression, LogisticRegression
from sklearn.multioutput import ClassifierChain
from sklearn.preprocessing import scale
from sklearn.tree import DecisionTreeClassifier, DecisionTreeRegressor
from sklearn.utils._response import _get_response_values, _get_response_values_binary
from sklearn.utils._testing import assert_allclose, assert_array_equal

X, y = load_iris(return_X_y=True)
# scale the data to avoid ConvergenceWarning with LogisticRegression
X = scale(X, copy=False)
X_binary, y_binary = X[:100], y[:100]


@pytest.mark.parametrize(
    "estimator, response_method",
    [
        (DecisionTreeRegressor(), "predict_proba"),
        (DecisionTreeRegressor(), ["predict_proba", "decision_function"]),
        (KMeans(n_clusters=2, n_init=1), "predict_proba"),
        (KMeans(n_clusters=2, n_init=1), ["predict_proba", "decision_function"]),
        (DBSCAN(), "predict"),
        (IsolationForest(random_state=0), "predict_proba"),
        (IsolationForest(random_state=0), ["predict_proba", "score"]),
    ],
)
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",
    [