diff --git a/pretab/compose/factory.py b/pretab/compose/factory.py index 8ced4d6..b8bb98b 100644 --- a/pretab/compose/factory.py +++ b/pretab/compose/factory.py @@ -10,6 +10,7 @@ import warnings +import numpy as np from sklearn.compose import ColumnTransformer from sklearn.impute import SimpleImputer from sklearn.pipeline import FeatureUnion, Pipeline @@ -49,6 +50,20 @@ _KNOT_SPLINE_METHODS = _BMI_SPLINE_METHODS | frozenset({"cubicspline", "naturalspline"}) +class _PositiveMinMaxScaler(MinMaxScaler): + """MinMaxScaler whose output is floored at ``feature_range[0]``. + + Rescales into ``(1e-3, 1)`` ahead of Box-Cox, which requires strictly + positive input. Fitted on the training data, a plain MinMaxScaler maps an + unseen value below the training minimum to ``<= 0``; flooring only the lower + end makes such values transform like the training minimum while every other + output (including values above the training maximum) is unchanged. + """ + + def transform(self, X): + return np.maximum(super().transform(X), self.feature_range[0]) + + def _filter_kwargs(allowed, kwargs): """Keep only the ``allowed`` keyword arguments that are present in ``kwargs``.""" return {key: kwargs[key] for key in allowed if key in kwargs} @@ -154,7 +169,7 @@ def get_numerical_transformer_steps( placement = _placement_kwargs(spec, kwargs) if method == "box-cox": - steps.append(("scale_positive", MinMaxScaler(feature_range=(1e-3, 1)))) + steps.append(("scale_positive", _PositiveMinMaxScaler(feature_range=(1e-3, 1)))) steps.append(("boxcox", cls(method="box-cox", **filtered))) elif method == "yeo-johnson": steps.append(("yeojohnson", cls(method="yeo-johnson", **filtered))) diff --git a/pretab/preprocessor.py b/pretab/preprocessor.py index d70473e..455cac8 100644 --- a/pretab/preprocessor.py +++ b/pretab/preprocessor.py @@ -331,6 +331,11 @@ class Preprocessor(TransformerMixin, BaseEstimator): ``"naturalspline"``, ``"pspline"``, ``"bspline"``, ``"mspline"``, ``"ispline"``, ``"fourier"``. + Box-Cox requires strictly positive input, so ``"box-cox"`` first rescales each column into + ``[1e-3, 1]`` with a min-max scaler fitted on the training data. That scaler's output is + floored at ``1e-3``: a value below the training minimum transforms like the minimum, while + values above the training maximum still extrapolate. + Available ``categorical_method`` values: ``"int"``, ``"one-hot"``, ``"onehot_from_ordinal"``, ``"pretrained"``, ``"none"``. The ``"pretrained"`` method requires the optional ``sentence-transformers`` dependency (``pip install "pretab[embeddings]"``). diff --git a/tests/integration/test_preprocessor.py b/tests/integration/test_preprocessor.py index df9d224..a722b7a 100644 --- a/tests/integration/test_preprocessor.py +++ b/tests/integration/test_preprocessor.py @@ -377,3 +377,40 @@ def test_unknown_feature_preprocessing_key_raises(sample_data): X, y = sample_data with pytest.raises(InvalidParamError, match="feature_preprocessing"): Preprocessor(feature_preprocessing={"nnum1": "minmax"}).fit(X, y) + + +# --- box-cox on unseen low values (issue #59) ------------------------------------- + + +@pytest.fixture +def boxcox_fitted(): + X = pd.DataFrame({"x": np.random.default_rng(0).normal(10, 2, 200)}) + return X, Preprocessor(numerical_method="box-cox").fit(X) + + +def test_boxcox_transforms_values_below_the_training_minimum(boxcox_fitted): + """Regression guard for issue #59: the positivity scaler mapped them to <= 0.""" + X, pre = boxcox_fitted + low = pre.transform(pd.DataFrame({"x": [X.x.min() - 5.0, X.x.min() - 0.5]})) + at_min = pre.transform(pd.DataFrame({"x": [X.x.min()]})) + assert np.isfinite(low).all() + np.testing.assert_allclose(low, np.repeat(at_min, 2, axis=0)) + + +def test_boxcox_still_extrapolates_above_the_training_maximum(boxcox_fitted): + X, pre = boxcox_fitted + out = pre.transform(pd.DataFrame({"x": [X.x.max(), X.x.max() + 3.0]})).ravel() + assert np.isfinite(out).all() + assert out[1] > out[0] + + +def test_boxcox_survives_cross_validation(): + from sklearn.datasets import make_regression + from sklearn.linear_model import Ridge + from sklearn.model_selection import cross_val_score + from sklearn.pipeline import make_pipeline + + data = make_regression(n_samples=500, n_features=3, noise=1.0, random_state=0) + X, y = data[0], data[1] + scores = cross_val_score(make_pipeline(Preprocessor(numerical_method="box-cox"), Ridge()), X, y, cv=5) + assert np.isfinite(scores).all()