Skip to content

MAINT introduce _get_response_values and _check_response_methods #23073

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
Mar 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions sklearn/calibration.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,17 @@
RegressorMixin,
clone,
MetaEstimatorMixin,
is_classifier,
)
from .preprocessing import label_binarize, LabelEncoder
from .utils import (
column_or_1d,
indexable,
check_matplotlib_support,
_safe_indexing,
)
from .utils._response import _get_response_values_binary

from .utils.multiclass import check_classification_targets
from .utils.multiclass import check_classification_targets, type_of_target
from .utils.parallel import delayed, Parallel
from .utils._param_validation import StrOptions, HasMethods, Hidden
from .utils.validation import (
Expand All @@ -44,12 +45,10 @@
check_consistent_length,
check_is_fitted,
)
from .utils import _safe_indexing
from .isotonic import IsotonicRegression
from .svm import LinearSVC
from .model_selection import check_cv, cross_val_predict
from .metrics._base import _check_pos_label_consistency
from .metrics._plot.base import _get_response


class CalibratedClassifierCV(ClassifierMixin, MetaEstimatorMixin, BaseEstimator):
Expand Down Expand Up @@ -1264,11 +1263,9 @@ def from_estimator(
method_name = f"{cls.__name__}.from_estimator"
check_matplotlib_support(method_name)

if not is_classifier(estimator):
raise ValueError("'estimator' should be a fitted classifier.")

y_prob, pos_label = _get_response(
X, estimator, response_method="predict_proba", pos_label=pos_label
check_is_fitted(estimator)
y_prob, pos_label = _get_response_values_binary(
estimator, X, response_method="predict_proba", pos_label=pos_label
)

name = name if name is not None else estimator.__class__.__name__
Expand Down Expand Up @@ -1381,9 +1378,15 @@ def from_predictions(
>>> disp = CalibrationDisplay.from_predictions(y_test, y_prob)
>>> plt.show()
"""
method_name = f"{cls.__name__}.from_estimator"
method_name = f"{cls.__name__}.from_predictions"
check_matplotlib_support(method_name)

target_type = type_of_target(y_true)
if target_type != "binary":
raise ValueError(
f"The target y is not binary. Got {target_type} type of target."
)

prob_true, prob_pred = calibration_curve(
y_true, y_prob, n_bins=n_bins, strategy=strategy, pos_label=pos_label
)
Expand Down
32 changes: 15 additions & 17 deletions sklearn/ensemble/_stacking.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,14 @@
from ..utils import Bunch
from ..utils.multiclass import check_classification_targets, type_of_target
from ..utils.metaestimators import available_if
from ..utils.validation import check_is_fitted
from ..utils.validation import column_or_1d
from ..utils.parallel import delayed, Parallel
from ..utils._param_validation import HasMethods, StrOptions
from ..utils.validation import _check_feature_names_in
from ..utils.validation import (
_check_feature_names_in,
_check_response_method,
check_is_fitted,
column_or_1d,
)


def _estimator_has(attr):
Expand Down Expand Up @@ -146,20 +149,15 @@ def _method_name(name, estimator, method):
if estimator == "drop":
return None
if method == "auto":
if getattr(estimator, "predict_proba", None):
return "predict_proba"
elif getattr(estimator, "decision_function", None):
return "decision_function"
else:
return "predict"
else:
if not hasattr(estimator, method):
raise ValueError(
"Underlying estimator {} does not implement the method {}.".format(
name, method
)
)
return method
method = ["predict_proba", "decision_function", "predict"]
try:
method_name = _check_response_method(estimator, method).__name__
except AttributeError as e:
raise ValueError(
f"Underlying estimator {name} does not implement the method {method}."
) from e

return method_name

def fit(self, X, y, sample_weight=None):
"""Fit the estimators.
Expand Down
8 changes: 6 additions & 2 deletions sklearn/ensemble/tests/test_stacking.py
Original file line number Diff line number Diff line change
Expand Up @@ -572,9 +572,13 @@ def test_stacking_prefit(Stacker, Estimator, stack_method, final_estimator, X, y

# mock out fit and stack_method to be asserted later
for _, estimator in estimators:
estimator.fit = Mock()
estimator.fit = Mock(name="fit")
stack_func = getattr(estimator, stack_method)
setattr(estimator, stack_method, Mock(side_effect=stack_func))
predict_method_mocked = Mock(side_effect=stack_func)
# Mocking a method will not provide a `__name__` while Python methods
# do and we are using it in `_get_response_method`.
predict_method_mocked.__name__ = stack_method
setattr(estimator, stack_method, predict_method_mocked)

stacker = Stacker(
estimators=estimators, cv="prefit", final_estimator=final_estimator
Expand Down
116 changes: 0 additions & 116 deletions sklearn/metrics/_plot/base.py

This file was deleted.

7 changes: 3 additions & 4 deletions sklearn/metrics/_plot/det_curve.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import scipy as sp

from .base import _get_response

from .. import det_curve
from .._base import _check_pos_label_consistency

from ...utils import check_matplotlib_support
from ...utils._response import _get_response_values_binary


class DetCurveDisplay:
Expand Down Expand Up @@ -168,9 +167,9 @@ def from_estimator(

name = estimator.__class__.__name__ if name is None else name

y_pred, pos_label = _get_response(
X,
y_pred, pos_label = _get_response_values_binary(
estimator,
X,
response_method,
pos_label=pos_label,
)
Expand Down
11 changes: 4 additions & 7 deletions sklearn/metrics/_plot/precision_recall_curve.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
from sklearn.base import is_classifier
from .base import _get_response

from .. import average_precision_score
from .. import precision_recall_curve
from .._base import _check_pos_label_consistency
from .._classification import check_consistent_length

from ...utils import check_matplotlib_support
from ...utils._response import _get_response_values_binary


class PrecisionRecallDisplay:
Expand Down Expand Up @@ -269,11 +267,10 @@ def from_estimator(
"""
method_name = f"{cls.__name__}.from_estimator"
check_matplotlib_support(method_name)
if not is_classifier(estimator):
raise ValueError(f"{method_name} only supports classifiers")
y_pred, pos_label = _get_response(
X,

y_pred, pos_label = _get_response_values_binary(
estimator,
X,
response_method,
pos_label=pos_label,
)
Expand Down
7 changes: 3 additions & 4 deletions sklearn/metrics/_plot/roc_curve.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
from .base import _get_response

from .. import auc
from .. import roc_curve
from .._base import _check_pos_label_consistency

from ...utils import check_matplotlib_support
from ...utils._response import _get_response_values_binary


class RocCurveDisplay:
Expand Down Expand Up @@ -231,9 +230,9 @@ def from_estimator(

name = estimator.__class__.__name__ if name is None else name

y_pred, pos_label = _get_response(
X,
y_pred, pos_label = _get_response_values_binary(
estimator,
X,
response_method=response_method,
pos_label=pos_label,
)
Expand Down
75 changes: 0 additions & 75 deletions sklearn/metrics/_plot/tests/test_base.py

This file was deleted.

Loading