From 7598206d317afd93a99045f6dbe457ca7446cb53 Mon Sep 17 00:00:00 2001 From: ChrisW09 <50968720+ChrisW09@users.noreply.github.com> Date: Fri, 16 Oct 2026 14:10:11 +0200 Subject: [PATCH] fix(search): default to the classification task for a classifier RepresentationSearchCV stratified its folds for a classifier but built every candidate Preprocessor with the default task="regression". Target-aware placement then crashed on string class labels and treated integer labels as a regression target. A classifier now defaults the preprocessors to task="classification"; an explicit task in preprocessor_params still wins. Fixes #77 --- docs/core_concepts/target_awareness.md | 4 +++- pretab/compose/search.py | 12 +++++++++-- tests/compose/test_search.py | 30 ++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 3 deletions(-) diff --git a/docs/core_concepts/target_awareness.md b/docs/core_concepts/target_awareness.md index d3eccf2..a71aaae 100644 --- a/docs/core_concepts/target_awareness.md +++ b/docs/core_concepts/target_awareness.md @@ -125,7 +125,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 7ce1d63..f01a140 100644 --- a/pretab/compose/search.py +++ b/pretab/compose/search.py @@ -47,7 +47,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`. @@ -74,9 +76,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"