Skip to content
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
4 changes: 3 additions & 1 deletion docs/core_concepts/target_awareness.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 10 additions & 2 deletions pretab/compose/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -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`.

Expand All @@ -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):
Expand Down
30 changes: 30 additions & 0 deletions tests/compose/test_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Loading