Skip to content
Open
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
3 changes: 2 additions & 1 deletion docs/core_concepts/target_awareness.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ ignore `y`, so they also fit inside a multi-output pipeline. Explicit spline
After fitting, a transformer reports whether it actually consumed the target through
`uses_target_`. This is the ground truth for an individual fit, and it flows into the
[`RepresentationSpec`](outputs_and_inspection.md) so a serialized representation records
whether it was supervised.
whether it was supervised. A B/M/I spline with explicit `knot_locations` reports `False` and
emits no `LeakageWarning`, even with `target_aware=True`, because no selector reads `y`.

```python
X = [[0.0], [1.0], [2.0]]
Expand Down
13 changes: 12 additions & 1 deletion pretab/expansion/spline/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ class BaseSplineTransformer(BasePreTabTransformer):
knot_locations : ndarray or None, default=None
Explicit internal knot locations applied to every feature. Takes priority
over ``target_aware`` placement and the automatic strategy, and overrides
``output_dim`` and the adaptive bounds. Repeated knots are merged; knots
``output_dim`` and the adaptive bounds. ``y`` is then not used, even with
``target_aware=True``, so the fit is not reported as target-aware. Repeated knots are merged; knots
on or outside a feature's fitted range are dropped for that feature with
a :class:`~pretab.exceptions.DataWarning`, without adding replacements.
A feature with ``K`` retained unique interior knots gets
Expand Down Expand Up @@ -210,6 +211,16 @@ def __init__(

_selector_spline_type: ClassVar[Literal["bspline", "mspline", "ispline"]] = "bspline"

@property
def is_supervised(self) -> bool:
"""Whether this transformer consumes ``y`` given its configuration.

Explicit ``knot_locations`` take priority over target-aware placement, so
no selector reads ``y`` then: the fit emits no leakage warning and
``uses_target_`` (and the representation spec) report ``False``.
"""
return bool(self.target_aware) and self.knot_locations is None

def _basis_to_knots(self, n_basis: int) -> int:
"""Convert a basis-function count to the number of internal knots."""
return basis_to_knots(n_basis, self.degree)
Expand Down
28 changes: 28 additions & 0 deletions tests/core/test_supervised_contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
from pretab.core.supervised import in_controlled_context, warn_target_leakage
from pretab.transformers import (
BSplineTransformer,
ISplineTransformer,
MSplineTransformer,
PLETransformer,
RBFExpansionTransformer,
)
Expand Down Expand Up @@ -127,3 +129,29 @@ def test_warn_helper_ignores_unsupervised(data):
with warnings.catch_warnings():
warnings.simplefilter("error", LeakageWarning)
warn_target_leakage(est, y)


@pytest.mark.parametrize("cls", [BSplineTransformer, MSplineTransformer, ISplineTransformer])
def test_explicit_knot_locations_are_not_target_aware(cls, data):
"""Explicit knots bypass the target-aware selector, so y is never read; the fit
still warned about leakage and reported uses_target_=True."""
X, y = data
knots = np.quantile(X[:, 0], [0.25, 0.5, 0.75])
transformer = cls(knot_locations=knots, target_aware=True, placement_strategy="cart")
with warnings.catch_warnings():
warnings.simplefilter("error", LeakageWarning)
transformer.fit(X, y)

assert transformer.is_supervised is False
assert transformer.uses_target_ is False
assert transformer.get_representation_spec().uses_target is False
unrelated = cls(knot_locations=knots, target_aware=True, placement_strategy="cart").fit(X, -y)
np.testing.assert_array_equal(transformer.transform(X), unrelated.transform(X))


def test_target_aware_spline_without_knots_stays_supervised(data):
X, y = data
transformer = BSplineTransformer(target_aware=True, placement_strategy="cart")
with pytest.warns(LeakageWarning):
transformer.fit(X, y)
assert transformer.uses_target_ is True
Loading