From 5a3a7beb566e3651db299bd899dc50f6d260e712 Mon Sep 17 00:00:00 2001 From: ChrisW09 <50968720+ChrisW09@users.noreply.github.com> Date: Fri, 16 Oct 2026 14:20:06 +0200 Subject: [PATCH] fix(feature-maps): drop the unsupervised relu center at the data maximum On the uniform / quantile path the ReLU expansion reused the feature maps' endpoint-inclusive centers, so its last ramp max(0, x - c) started at the training maximum and was identically zero on every training row: output_dim=k gave only k - 1 informative columns. The ReLU now places output_dim + 1 range-spanning locations and drops the right endpoint, so the first center stays at the minimum and no ramp starts at the training maximum. A zero-range feature keeps output_dim equal centers. RBF / sigmoid / tanh and the target-aware path are unchanged. The ReLU docstring and the functional-expansions page describe the placement. Fixes #83 --- docs/representations/functional_expansions.md | 5 ++- pretab/expansion/functional/base.py | 10 +++++- pretab/expansion/functional/relu.py | 16 +++++++++ .../test_reluexpansion_transformer.py | 36 +++++++++++++++++++ 4 files changed, 65 insertions(+), 2 deletions(-) diff --git a/docs/representations/functional_expansions.md b/docs/representations/functional_expansions.md index 35a5586..98fa2bc 100644 --- a/docs/representations/functional_expansions.md +++ b/docs/representations/functional_expansions.md @@ -60,7 +60,10 @@ $$ $$ ReLU -: Piecewise-linear ramps. Excellent for sharp, threshold-like effects. +: Piecewise-linear ramps. Excellent for sharp, threshold-like effects. With unsupervised +placement the centers are the first `output_dim` of `output_dim + 1` range-spanning points: the +first ramp starts at the training minimum, and the right endpoint is dropped because a ramp +starting at the training maximum is zero on every training row. Sigmoid and Tanh : Smooth saturating steps. `scale` controls the steepness of the transition: **smaller** values diff --git a/pretab/expansion/functional/base.py b/pretab/expansion/functional/base.py index 95432f9..81ce176 100644 --- a/pretab/expansion/functional/base.py +++ b/pretab/expansion/functional/base.py @@ -117,9 +117,17 @@ def fit(self, X, y=None): for i in range(X.shape[1]): if np.isnan(X[:, i]).all(): raise PretabDataError(f"Feature at index {i} has only NaN values") - self.centers_.append(adapter.get_centers(X[:, i], y_place, min_centers, max_centers)) + if self.target_aware: + centers = adapter.get_centers(X[:, i], y_place, min_centers, max_centers) + else: + centers = self._unsupervised_centers(adapter, X[:, i], n_centers) + self.centers_.append(centers) return self + def _unsupervised_centers(self, adapter, x, n_centers): + """Return ``n_centers`` quantile / uniform centers spanning the feature range.""" + return adapter.get_centers(x, None, n_centers, n_centers) + def transform(self, X): """Expand every feature against its centers and stack the results.""" check_is_fitted(self, "centers_") diff --git a/pretab/expansion/functional/relu.py b/pretab/expansion/functional/relu.py index 6b2496a..bd08a0a 100644 --- a/pretab/expansion/functional/relu.py +++ b/pretab/expansion/functional/relu.py @@ -63,6 +63,12 @@ class ReLUExpansionTransformer(BaseCenterExpansion): producing ``output_dim`` new features per original feature on the non-target-aware path; the target-aware default may place a data-driven number. + On the unsupervised (``"uniform"`` / ``"quantile"``) path the centers are the + first ``output_dim`` of ``output_dim + 1`` range-spanning locations: the first + center is the training minimum (a linear term), and the right endpoint is + dropped because a ramp starting at the training maximum is zero on every + training row. + Examples -------- >>> import numpy as np @@ -78,6 +84,16 @@ class ReLUExpansionTransformer(BaseCenterExpansion): _feature_suffix_value = "relu" _representation_family = "relu" + def _unsupervised_centers(self, adapter, x, n_centers): + """Return ``n_centers`` centers that each start a ramp inside the data. + + A ramp ``max(0, x - c)`` centered at the training maximum is identically + zero on the training data, so ``n_centers + 1`` range-spanning locations + are placed and the right endpoint is dropped (the first center stays at + the minimum, giving the linear term). + """ + return adapter.get_centers(x, None, n_centers + 1, n_centers + 1)[:-1] + def __init__( self, output_dim=UNSET, diff --git a/tests/expansion/functional/test_reluexpansion_transformer.py b/tests/expansion/functional/test_reluexpansion_transformer.py index 8004c5f..2ac0c42 100644 --- a/tests/expansion/functional/test_reluexpansion_transformer.py +++ b/tests/expansion/functional/test_reluexpansion_transformer.py @@ -1,9 +1,11 @@ import warnings import numpy as np +import pandas as pd import pytest from sklearn.utils.validation import check_is_fitted +from pretab import Preprocessor from pretab.transformers import ReLUExpansionTransformer @@ -70,3 +72,37 @@ def test_relu_feature_mismatch(X_multi_feature, y_regression): transformer.fit(X_multi_feature[:, :2], y_regression) with pytest.raises(ValueError, match="is expecting"): transformer.transform(X_multi_feature) + + +@pytest.mark.parametrize("placement_strategy", ["uniform", "quantile"]) +def test_unsupervised_relu_has_no_dead_column(placement_strategy): + """The last center sat at the training maximum, so max(0, x - c) was identically + zero on the training data and only output_dim - 1 columns carried signal.""" + X = np.random.default_rng(0).uniform(0, 10, (500, 1)) + transformer = ReLUExpansionTransformer(output_dim=6, placement_strategy=placement_strategy).fit(X) + out = transformer.transform(X) + assert out.shape == (500, 6) + assert (out.max(axis=0) > 0).all() + assert np.linalg.matrix_rank(out) == 6 + assert transformer.centers_[0][0] == X.min() + assert transformer.centers_[0][-1] < X.max() + # A zero-range feature has no interior: it keeps output_dim (equal) centers. + X_const = np.full((20, 1), 3.0) + constant = ReLUExpansionTransformer(output_dim=4, placement_strategy=placement_strategy).fit(X_const) + np.testing.assert_array_equal(constant.centers_[0], np.full(4, 3.0)) + + +def test_unsupervised_relu_uniform_centers_drop_the_right_endpoint(): + X = np.random.default_rng(0).uniform(0, 10, (500, 1)) + transformer = ReLUExpansionTransformer(output_dim=6, placement_strategy="uniform").fit(X) + np.testing.assert_allclose(transformer.centers_[0], np.linspace(X.min(), X.max(), 7)[:-1]) + + +@pytest.mark.parametrize("placement_strategy", ["uniform", "quantile"]) +def test_preprocessor_relu_has_no_constant_column(placement_strategy): + X = pd.DataFrame({"a": np.random.default_rng(0).uniform(0, 10, 500)}) + pre = Preprocessor(numerical_method="relu", target_aware=False, placement_strategy=placement_strategy, output_dim=7) + out = np.asarray(pre.fit_transform(X), dtype=float) + assert out.shape == (500, 7) + assert (out.std(axis=0) > 0).all() + assert np.linalg.matrix_rank(out) == 7