diff --git a/docs/core_concepts/target_awareness.md b/docs/core_concepts/target_awareness.md index bdd9dd7..df17127 100644 --- a/docs/core_concepts/target_awareness.md +++ b/docs/core_concepts/target_awareness.md @@ -160,7 +160,9 @@ selection, and doing it carelessly (for example scoring each candidate on the sa to fit it) leaks information the same way an unguarded supervised transformer does. `RepresentationSearchCV` cross-validates a downstream estimator over a set of candidate `numerical_method` values, refits the best one on all the data, and keeps every candidate's -scoring honest by fitting a fresh `Preprocessor` per fold. +scoring honest by fitting a fresh `Preprocessor` per fold. With a classifier as the estimator, +every `Preprocessor` uses `task="classification"`, so target-aware methods place against the +class labels; a `task` set in `preprocessor_params` takes precedence. ```python import numpy as np diff --git a/pretab/compose/search.py b/pretab/compose/search.py index e5a3428..df03818 100644 --- a/pretab/compose/search.py +++ b/pretab/compose/search.py @@ -48,7 +48,9 @@ class RepresentationSearchCV(BaseEstimator): Scoring passed to :func:`sklearn.metrics.check_scoring`; ``None`` uses the estimator's ``score`` method. preprocessor_params : dict or None, default=None - Extra keyword arguments forwarded to every :class:`Preprocessor`. + Extra keyword arguments forwarded to every :class:`Preprocessor`. When + ``estimator`` is a classifier, ``task`` defaults to ``"classification"`` so + target-aware placement treats ``y`` as class labels. random_state : int or None, default=None Seed forwarded to each :class:`Preprocessor`. @@ -75,9 +77,15 @@ def __init__(self, estimator, methods, *, cv=5, scoring=None, preprocessor_param self.random_state = random_state def _make_preprocessor(self, method): - """Build a Preprocessor for ``method`` with the shared parameters.""" + """Build a Preprocessor for ``method`` with the shared parameters. + + Target-aware placement follows the downstream estimator: a classifier makes + it ``task="classification"`` unless ``preprocessor_params`` sets ``task``. + """ params = dict(self.preprocessor_params or {}) params.setdefault("random_state", self.random_state) + if is_classifier(self.estimator): + params.setdefault("task", "classification") return Preprocessor(numerical_method=method, **params) def fit(self, X, y=None): diff --git a/tests/compose/test_search.py b/tests/compose/test_search.py index 7268892..c3e065f 100644 --- a/tests/compose/test_search.py +++ b/tests/compose/test_search.py @@ -124,3 +124,33 @@ def test_get_params_and_clone_preserve_config(): cloned = clone(search) assert isinstance(cloned, RepresentationSearchCV) assert cloned.get_params()["cv"] == 3 + + +@pytest.fixture +def class_data(): + rng = np.random.default_rng(0) + X = pd.DataFrame({"x": rng.uniform(-3, 3, 300)}) + return X, np.where(np.sin(X["x"]) > 0, "pos", "neg") + + +def test_classifier_search_places_against_class_labels(class_data): + """A classifier made the search keep task="regression": string labels crashed the + target-aware placement and integer labels were treated as a regression target.""" + X, y = class_data + search = RepresentationSearchCV(LogisticRegression(), methods=["minmax", "ple"], cv=3, random_state=0).fit(X, y) + assert search.best_preprocessor_.task == "classification" + assert set(search.predict(X)) <= {"pos", "neg"} + + +def test_classifier_search_keeps_an_explicit_task(class_data): + X, y = class_data + search = RepresentationSearchCV( + LogisticRegression(), methods=["minmax"], cv=3, preprocessor_params={"task": "regression"} + ).fit(X, (y == "pos").astype(int)) + assert search.best_preprocessor_.task == "regression" + + +def test_regressor_search_keeps_the_regression_task(nonlinear_data): + X, y = nonlinear_data + search = RepresentationSearchCV(LinearRegression(), methods=["minmax"], cv=3).fit(X, y) + assert search.best_preprocessor_.task == "regression"