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

Code before:
    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(
    "estimator, Xy, response_method",
    [
        (
            DecisionTreeRegressor(),
            make_regression(n_samples=10, random_state=0),
            "predict_proba",
        ),
        (
            DecisionTreeRegressor(),
            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),

Review comment (suggestion):
Wouldn't they all work with n_samples=50? Then `Xy` could be taken out of the parametrization, because this does not directly impact the test.

Code after:
    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",
    [
        (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"),