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
7 changes: 5 additions & 2 deletions docs/core_concepts/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,9 @@ regression and classification differently.
| `"adaptive"` | task-dependent | `"int"` | - | `True` | `7` | `15` |

`numerical_method` resolves to `"bspline"` when `task="regression"` (the default) and to
`"ple"` when `task="classification"`, for every preset:
`"ple"` when `task="classification"`, for every preset. `task` is matched exactly: any other
value, such as `"Regression"`, raises an `InvalidParamError` from `fit` and
`get_resolved_config()` instead of falling back to the classification preset.

```python
standard_regression = Preprocessor(preset="standard", task="regression")
Expand Down Expand Up @@ -195,7 +197,8 @@ The resolution order is deterministic. Later layers win.

```{warning}
Configuration is validated at `fit` time, not silently coerced. An invalid combination, such
as a method that requires the target used with `target_aware=False`, raises a typed error.
as a method that requires the target used with `target_aware=False`, or a `task` other than
`"regression"` / `"classification"`, raises a typed error.
This is intentional: it surfaces mistakes early rather than producing a quietly wrong
representation.
```
Expand Down
19 changes: 11 additions & 8 deletions pretab/compose/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
:class:`PreprocessorConfig` is the frozen, canonical view of the user-supplied
Preprocessor parameters. It normalizes the global method names (resolving
aliases and separator/case variants, mapping ``None`` to ``"none"``) and
validates the global ``target_aware`` / ``placement_strategy`` contract up front.
The user's original constructor arguments stay untouched on the estimator (as
scikit-learn requires); this object is the internal, normalized counterpart the
composition layer consumes.
validates the global ``target_aware`` / ``placement_strategy`` contract and the
``task`` up front. The user's original constructor arguments stay untouched on
the estimator (as scikit-learn requires); this object is the internal, normalized
counterpart the composition layer consumes.

Per-column overrides in ``feature_preprocessing`` are kept verbatim because the
namespace they resolve in (numerical vs categorical) depends on the column type,
Expand All @@ -18,7 +18,7 @@

from dataclasses import dataclass

from ..core.parameters import validate_placement
from ..core.parameters import validate_placement, validate_task
from ..exceptions import invalid_param_error
from .registry import (
CATEGORICAL_ALIASES,
Expand Down Expand Up @@ -47,8 +47,8 @@ class PreprocessorConfig:
"""Frozen, normalized configuration derived from Preprocessor parameters.

Built via :meth:`from_params`, which normalizes the global method names and
validates the placement contract. All other knobs are carried through as-is
for the factory and orchestration layers.
validates the placement contract and the task. All other knobs are carried
through as-is for the factory and orchestration layers.
"""

numerical_method: str
Expand Down Expand Up @@ -103,9 +103,12 @@ def from_params(
------
InvalidParamError
If the ``target_aware`` / ``placement_strategy`` combination is
invalid (via :func:`~pretab.core.parameters.validate_placement`).
invalid (via :func:`~pretab.core.parameters.validate_placement`), or
``task`` is not ``"regression"`` / ``"classification"`` (via
:func:`~pretab.core.parameters.validate_task`).
"""
validate_placement(target_aware, placement_strategy)
validate_task(task, "Preprocessor")
if missing_policy is not None and missing_policy not in MISSING_POLICIES:
raise invalid_param_error(
"Preprocessor",
Expand Down
47 changes: 46 additions & 1 deletion pretab/core/parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
import warnings
from typing import Any, ClassVar

from ..exceptions import InvalidParamError
from ..exceptions import InvalidParamError, invalid_param_error
from ._typing import Task


class _Unset:
Expand Down Expand Up @@ -76,6 +77,50 @@ def validate_placement(target_aware: bool, placement_strategy: str) -> None:
raise InvalidParamError("When target_aware=False, placement_strategy must be 'uniform' or 'quantile'.")


#: Supervised tasks accepted by every ``task`` parameter.
TASKS: tuple[Task, ...] = ("regression", "classification")


def validate_task(task, estimator: str, *, allow_none: bool = False) -> Task | None:
"""Check that ``task`` names a supported supervised task and return it.

The match is exact, so a near miss such as ``"Regression"`` is rejected
instead of being treated as classification by the target-aware placement.
Like :func:`validate_placement`, this is called at ``fit`` time, not at
construction.

Parameters
----------
task : object
The ``task`` value to check.
estimator : str
Name of the estimator owning the parameter, used in the error message.
allow_none : bool, default=False
Whether ``None`` is accepted (the spline families and location selectors
treat it as ``"regression"``).

Returns
-------
{"regression", "classification"} or None
The validated ``task``.

Raises
------
InvalidParamError
If ``task`` is not ``"regression"`` or ``"classification"`` (or ``None``
when ``allow_none`` is True).
"""
if task is None and allow_none:
return None
if isinstance(task, str) and task in TASKS:
return task
if allow_none:
constraint = "must be 'regression', 'classification', or None (treated as 'regression')"
else:
constraint = "must be 'regression' or 'classification'"
raise invalid_param_error(estimator, "task", task, constraint, valid=TASKS)


#: Shared parameter names with a short description of what each one controls.
CANONICAL_PARAMS: dict[str, str] = {
"output_dim": "Number of non-bias output columns produced per input feature.",
Expand Down
37 changes: 18 additions & 19 deletions pretab/core/selectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

from ..exceptions import IncompatibleParamsError, OptionalDependencyError
from .knots import quantile_knots, select_knots
from .parameters import validate_task

Task = Literal["regression", "classification"]

Expand Down Expand Up @@ -68,8 +69,8 @@ def select(
Input feature values for one feature.
y : np.ndarray of shape (n_samples,)
Target values. Required -- these selectors are target aware.
task : {"regression", "classification"}, optional
Type of prediction task. Defaults to ``"regression"``.
task : {"regression", "classification"} or None, default="regression"
Type of prediction task. ``None`` is treated as ``"regression"``.
min_count : int
Minimum number of locations to return.
max_count : int
Expand All @@ -79,10 +80,15 @@ def select(
-------
locations : np.ndarray
Sorted array of selected locations.

Raises
------
InvalidParamError
If ``task`` is not ``"regression"``, ``"classification"`` or ``None``.
"""
if y is None:
raise IncompatibleParamsError(f"{type(self).__name__} requires y to select locations.")
task = task or "regression"
task = validate_task(task, type(self).__name__, allow_none=True) or "regression"

x = np.asarray(x)
if x.ndim == 1:
Expand Down Expand Up @@ -206,20 +212,13 @@ def __init__(
self.min_samples_floor = min_samples_split

def _ordered_candidates(self, x_valid: np.ndarray, y_valid: np.ndarray, task: Task) -> tuple[list[float], object]:
if task == "regression":
tree = DecisionTreeRegressor(
max_depth=self.max_tree_depth,
min_samples_split=self.min_samples_split,
min_samples_leaf=self.min_samples_leaf,
random_state=self.random_state,
)
else:
tree = DecisionTreeClassifier(
max_depth=self.max_tree_depth,
min_samples_split=self.min_samples_split,
min_samples_leaf=self.min_samples_leaf,
random_state=self.random_state,
)
tree_class = DecisionTreeClassifier if task == "classification" else DecisionTreeRegressor
tree = tree_class(
max_depth=self.max_tree_depth,
min_samples_split=self.min_samples_split,
min_samples_leaf=self.min_samples_leaf,
random_state=self.random_state,
)

tree.fit(x_valid, y_valid)
split_points = self._extract_split_points(tree, x_valid)
Expand Down Expand Up @@ -339,8 +338,8 @@ def _ordered_candidates(self, x_valid: np.ndarray, y_valid: np.ndarray, task: Ta
lgb = self._import_lightgbm()

params = {
"objective": "regression" if task == "regression" else "binary",
"metric": "rmse" if task == "regression" else "binary_logloss",
"objective": "binary" if task == "classification" else "regression",
"metric": "binary_logloss" if task == "classification" else "rmse",
"num_leaves": 2**self.max_depth,
"max_depth": self.max_depth,
"learning_rate": self.learning_rate,
Expand Down
4 changes: 2 additions & 2 deletions pretab/core/supervised.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
PretabDataError,
)
from ._typing import TransformerLike
from .parameters import validate_task
from .representation import RepresentationSpecMixin

__all__ = ["CrossFittedTransformer", "in_controlled_context", "warn_target_leakage"]
Expand Down Expand Up @@ -139,8 +140,7 @@ def _fit_full(self, X, y):
raise IncompatibleParamsError("CrossFittedTransformer requires y at fit time; got y=None.")
if not isinstance(self.n_folds, (int, np.integer)) or self.n_folds < 2:
raise InvalidParamError(f"n_folds must be an integer >= 2; got {self.n_folds!r}.")
if self.task not in ("regression", "classification"):
raise InvalidParamError(f"task must be 'regression' or 'classification'; got {self.task!r}.")
validate_task(self.task, type(self).__name__)
X_arr = np.asarray(X)
if X_arr.ndim == 1:
X_arr = X_arr.reshape(-1, 1)
Expand Down
7 changes: 3 additions & 4 deletions pretab/encoding/numerical/ple.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from sklearn.utils.validation import check_array, check_is_fitted

from ...core.adaptive import AdaptiveResolutionMixin
from ...core.parameters import UNSET, AliasResolverMixin
from ...core.parameters import UNSET, AliasResolverMixin, validate_task
from ...core.representation import RepresentationSpecMixin
from ...core.supervised import warn_target_leakage
from ...exceptions import (
Expand Down Expand Up @@ -182,8 +182,7 @@ def fit(self, X, y=None):
max_bins_req = self._resolve_param("max_output_dim", default=None)
min_bins, max_bins = self._resolve_bin_bounds(n_bins, min_bins_req, max_bins_req)

if self.task not in ("regression", "classification"):
raise InvalidParamError(f"Unsupported task: {self.task}. Use 'regression' or 'classification'.")
task = validate_task(self.task, type(self).__name__)

if self.placement_strategy not in ("cart", "lightgbm"):
raise InvalidParamError(
Expand All @@ -198,7 +197,7 @@ def fit(self, X, y=None):
# exactly ``output_dim`` bins, adaptive clamps it into ``[min, max]``.
adapter = PLEPlacementAdapter(
placement_strategy=self.placement_strategy,
task=self.task,
task=task,
random_state=self.random_state,
)
min_thresholds = max(0, min_bins - 1)
Expand Down
7 changes: 3 additions & 4 deletions pretab/expansion/functional/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from sklearn.utils.validation import check_is_fitted

from ...core.base import BasePreTabTransformer
from ...core.parameters import UNSET, validate_placement
from ...core.parameters import UNSET, validate_placement, validate_task
from ...core.supervised import warn_target_leakage
from ...exceptions import (
IncompatibleParamsError,
Expand Down Expand Up @@ -84,8 +84,7 @@ def fit(self, X, y=None):
warn_target_leakage(self, y)
placement_strategy = self._resolve_placement_strategy()
validate_placement(self.target_aware, placement_strategy)
if self.task not in ("regression", "classification"):
raise InvalidParamError(f"Invalid task. Choose 'regression' or 'classification'. Got {self.task!r}.")
task = validate_task(self.task, type(self).__name__)
n_centers = self._resolve_param("output_dim", default=6)
min_req = self._resolve_param("min_output_dim", default=None)
max_req = self._resolve_param("max_output_dim", default=None)
Expand All @@ -105,7 +104,7 @@ def fit(self, X, y=None):
adapter = RBFPlacementAdapter(
target_aware=self.target_aware,
placement_strategy=placement_strategy,
task=self.task,
task=task,
random_state=self.random_state,
)
if self.target_aware and self.adaptive:
Expand Down
7 changes: 5 additions & 2 deletions pretab/expansion/spline/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
select_knots,
uniform_knots,
)
from ...core.parameters import UNSET, validate_placement
from ...core.parameters import UNSET, validate_placement, validate_task
from ...core.policy import RepresentationPolicy, resolve_out_of_range
from ...core.supervised import warn_target_leakage
from ...exceptions import (
Expand Down Expand Up @@ -78,7 +78,9 @@ class BaseSplineTransformer(BasePreTabTransformer):
evenly across the range, ``"quantile"`` places them at data quantiles.

task : {"regression", "classification"} or None, default=None
Task passed to the target-aware selector when ``target_aware=True``.
Task passed to the target-aware selector when ``target_aware=True``. ``None`` is
treated as ``"regression"``; any other value raises
:class:`~pretab.exceptions.InvalidParamError` at ``fit``.

adaptive : bool, default=False
If True, the per-feature output dimension may vary within
Expand Down Expand Up @@ -266,6 +268,7 @@ def fit(self, X, y=None):
"""Determine per-feature knot vectors."""
warn_target_leakage(self, y)
validate_placement(self.target_aware, self.placement_strategy)
validate_task(self.task, type(self).__name__, allow_none=True)
n_basis = self._resolve_param("output_dim", default=6)
min_basis_req = self._resolve_param("min_output_dim", default=None)
max_basis_req = self._resolve_param("max_output_dim", default=None)
Expand Down
7 changes: 5 additions & 2 deletions pretab/expansion/spline/cubic_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from sklearn.base import BaseEstimator, TransformerMixin
from sklearn.utils.validation import check_is_fitted

from ...core.parameters import UNSET, validate_placement
from ...core.parameters import UNSET, validate_placement, validate_task
from ...core.policy import RepresentationPolicy, resolve_out_of_range
from ...core.supervised import warn_target_leakage
from ...exceptions import InvalidParamError
Expand Down Expand Up @@ -56,7 +56,9 @@ class CubicRegressionSplineTransformer(SplineBasisMixin, TransformerMixin, BaseE
across the range, ``"quantile"`` places them at evenly spaced data quantiles.

task : {"regression", "classification"} or None, default=None
Task forwarded to the target-aware selector when ``target_aware=True``.
Task forwarded to the target-aware selector when ``target_aware=True``. ``None`` is
treated as ``"regression"``; any other value raises
:class:`~pretab.exceptions.InvalidParamError` at ``fit``.

adaptive : bool, default=False
If True (with ``target_aware=True``), the per-feature output dimension may
Expand Down Expand Up @@ -166,6 +168,7 @@ def _bspline_basis(self, x, knots):
def fit(self, X, y=None):
warn_target_leakage(self, y)
validate_placement(self.target_aware, self.placement_strategy)
validate_task(self.task, type(self).__name__, allow_none=True)
X = self._validate_allow_nan(X, reset=True)
output_dim = self._resolve_param("output_dim", default=6)

Expand Down
7 changes: 5 additions & 2 deletions pretab/expansion/spline/natural_cubic.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from sklearn.base import BaseEstimator, TransformerMixin
from sklearn.utils.validation import check_is_fitted

from ...core.parameters import UNSET, validate_placement
from ...core.parameters import UNSET, validate_placement, validate_task
from ...core.policy import RepresentationPolicy, resolve_out_of_range
from ...core.supervised import warn_target_leakage
from ...exceptions import InvalidParamError
Expand Down Expand Up @@ -58,7 +58,9 @@ class NaturalCubicSplineTransformer(SplineBasisMixin, TransformerMixin, BaseEsti
data quantiles.

task : {"regression", "classification"} or None, default=None
Task forwarded to the target-aware selector when ``target_aware=True``.
Task forwarded to the target-aware selector when ``target_aware=True``. ``None`` is
treated as ``"regression"``; any other value raises
:class:`~pretab.exceptions.InvalidParamError` at ``fit``.

adaptive : bool, default=False
If True (with ``target_aware=True``), the per-feature output dimension may
Expand Down Expand Up @@ -176,6 +178,7 @@ def d(k):
def fit(self, X, y=None):
warn_target_leakage(self, y)
validate_placement(self.target_aware, self.placement_strategy)
validate_task(self.task, type(self).__name__, allow_none=True)
X = self._validate_allow_nan(X, reset=True)
output_dim = self._resolve_param("output_dim", default=6)

Expand Down
Loading
Loading