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

Code before:
    Ensure `_get_response_values` doesn't raise spurious warning.

    "The number of unique classes is greater than > 50% of samples"
    warning should not be raised when calling `type_of_target(classes_)`.

    Non-regression test for issue #31583.
    """
    X = np.random.RandomState(0).randn(120, 3)
    # 30 classes, less than 50% of number of samples
    y = np.repeat(np.arange(30), 4)

    clf = LogisticRegression().fit(X, y)

    with warnings.catch_warnings():
        warnings.simplefilter("error", UserWarning)

        _get_response_values(clf, X, response_method="predict_proba")


@pytest.mark.parametrize(
    "estimator, response_method, target_type, expected_shape",
    [
        (LogisticRegression(), "predict", "binary", (10,)),
        (LogisticRegression(), "predict_proba", "binary", (10,)),
        (LogisticRegression(), "decision_function", "binary", (10,)),
        (LogisticRegression(), "predict", "multiclass", (10,)),
        (LogisticRegression(), "predict_proba", "multiclass", (10, 10)),
        (LogisticRegression(), "decision_function", "multiclass", (10, 10)),
        (ClassifierChain(LogisticRegression()), "predict", "multilabel", (10, 2)),
        (ClassifierChain(LogisticRegression()), "predict_proba", "multilabel", (10, 2)),
        (
            ClassifierChain(LogisticRegression()),
            "decision_function",
            "multilabel",
            (10, 2),
        ),
        (IsolationForest(), "predict", "binary", (10,)),
        (IsolationForest(), "predict", "multiclass", (10,)),
        (DecisionTreeRegressor(), "predict", "binary", (10,)),
        (DecisionTreeRegressor(), "predict", "multiclass", (10,)),
    ],
)
def test_response_values_output_shape_(
    estimator, response_method, target_type, expected_shape
):
    """
    Check that output shape corresponds to docstring description

    - for binary classification, it is a 1d array of shape `(n_samples,)`;
    - for multiclass classification
        - with response_method="predict", it is a 1d array of shape `(n_samples,)`;

Review comment (suggestion):
This is the test that was inconsistent with the previous version of the docstring (which suggested that it should be `(10,4)`)

Code after:
    Ensure `_get_response_values` doesn't raise spurious warning.

    "The number of unique classes is greater than > 50% of samples"
    warning should not be raised when calling `type_of_target(classes_)`.

    Non-regression test for issue #31583.
    """
    X = np.random.RandomState(0).randn(120, 3)
    # 30 classes, less than 50% of number of samples
    y = np.repeat(np.arange(30), 4)

    clf = LogisticRegression().fit(X, y)

    with warnings.catch_warnings():
        warnings.simplefilter("error", UserWarning)

        _get_response_values(clf, X, response_method="predict_proba")


@pytest.mark.parametrize(
    "estimator, response_method, target_type, expected_shape",
    [
        (LogisticRegression(), "predict", "binary", (10,)),
        (LogisticRegression(), "predict_proba", "binary", (10,)),
        (LogisticRegression(), "decision_function", "binary", (10,)),
        (LogisticRegression(), "predict", "multiclass", (10,)),
        (LogisticRegression(), "predict_proba", "multiclass", (10, 4)),
        (LogisticRegression(), "decision_function", "multiclass", (10, 4)),
        (ClassifierChain(LogisticRegression()), "predict", "multilabel", (10, 2)),
        (ClassifierChain(LogisticRegression()), "predict_proba", "multilabel", (10, 2)),
        (
            ClassifierChain(LogisticRegression()),
            "decision_function",
            "multilabel",
            (10, 2),
        ),
        (IsolationForest(), "predict", "binary", (10,)),
        (IsolationForest(), "predict", "multiclass", (10,)),
        (DecisionTreeRegressor(), "predict", "binary", (10,)),
        (DecisionTreeRegressor(), "predict", "multiclass", (10,)),
    ],
)
def test_response_values_output_shape_(
    estimator, response_method, target_type, expected_shape
):
    """
    Check that output shape corresponds to docstring description

    - for binary classification, it is a 1d array of shape `(n_samples,)`;
    - for multiclass classification
        - with response_method="predict", it is a 1d array of shape `(n_samples,)`;