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 bb8a107..80c7d69 100644 --- a/pretab/expansion/functional/base.py +++ b/pretab/expansion/functional/base.py @@ -116,9 +116,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 7829a64..624ffda 100644 --- a/pretab/expansion/functional/relu.py +++ b/pretab/expansion/functional/relu.py @@ -65,6 +65,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 @@ -80,6 +86,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 301f78d..24767b1 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