diff --git a/docs/core_concepts/target_awareness.md b/docs/core_concepts/target_awareness.md index 94a6453..5a82488 100644 --- a/docs/core_concepts/target_awareness.md +++ b/docs/core_concepts/target_awareness.md @@ -80,14 +80,16 @@ held-out fold, so every training row is transformed by a model that never saw it the training data, since there is no leakage risk there. A wrapped `Preprocessor` is not refit from scratch on each fold: every fold reuses its all-data -fit and refits only the blocks whose representations use `y`. Column types and the blocks that -do not use `y`, including their category vocabularies, are shared. Their out-of-fold values match `transform` exactly; +fit and refits only the target-aware representations and subsequent pipeline steps. Column +types, target-free blocks and their category vocabularies, imputers, scalers before the +representation, and missing-indicator branches are shared. Target-free values match `transform` exactly; target-aware values can differ because they exclude the held-out targets. -The entire target-aware block is currently refit, including its imputer and missing-indicator -branch. Each refitted block must retain its all-data output width. If adaptive sizing or -fold-specific missing-indicator columns change that width, cross-fitting raises an error -instead of mixing incompatible feature layouts. +Missing indicators stay as raw columns next to the representation; they are not expanded into +basis functions and do not disappear when a fold's training rows contain no missing values. +Each refitted block must still retain its all-data output width. If a representation's +data-dependent sizing changes that width, cross-fitting raises an error instead of mixing +incompatible feature layouts. ```python import numpy as np diff --git a/pretab/compose/inspection.py b/pretab/compose/inspection.py index 9c24c69..f7abc20 100644 --- a/pretab/compose/inspection.py +++ b/pretab/compose/inspection.py @@ -8,11 +8,13 @@ """ import copy -from typing import Any +from typing import Any, cast import numpy as np +from sklearn.base import clone from sklearn.pipeline import FeatureUnion, Pipeline +from ..core._typing import TransformerLike from ..core.logging import get_logger from ..core.representation import FeatureLineage from .registry import TRANSFORMER_REGISTRY @@ -28,6 +30,7 @@ "clean_feature_names", "feature_names_out", "get_output_slices", + "refit_block_representation", "representation_leaf", ] @@ -364,20 +367,25 @@ def build_transformer_summary(numerical_info, categorical_info, embedding_info): } -def _resolve_block_representation(pipeline, columns): - """Return ``(family, component, uses_target, is_interaction)`` for a block. +def _locate_block_representation(pipeline, columns): + """Return ``(step_index, (family, component, uses_target, is_interaction))`` for a block. The representation-bearing step is the last pipeline step exposing a ``get_representation_spec`` (a PreTab transformer), a known scikit-learn step name, or a registered method that consumes the target; helper steps such as imputers and float casts are skipped. ``columns`` names the spec's - inputs; with None the step names its own. + inputs; with None the step names its own. ``step_index`` is that step's + position in ``pipeline.steps`` (0 for a bare transformer), or None when the + block has no representation step. Known scikit-learn names are a fallback: + trailing scalers must not hide a representation's declared target use. """ steps = pipeline.steps if hasattr(pipeline, "steps") else [("_", pipeline)] - for step_name, transformer in reversed(steps): + legacy_step = None + for index in reversed(range(len(steps))): + step_name, transformer = steps[index] if hasattr(transformer, "get_representation_spec"): spec = transformer.get_representation_spec(input_features=None if columns is None else list(columns)) - return spec.family, spec.component_kind, spec.uses_target, spec.is_interaction + return index, (spec.family, spec.component_kind, spec.uses_target, spec.is_interaction) registered = TRANSFORMER_REGISTRY.get(step_name) if ( registered is not None @@ -389,11 +397,16 @@ def _resolve_block_representation(pipeline, columns): # cross-fitting refits it per fold instead of reusing the all-data fit. uses_target = registered.target_usage == "required" or bool(getattr(transformer, "target_aware", False)) component = "category" if registered.is_categorical else "basis" - return step_name, component, uses_target, registered.is_multivariate - if step_name in _STEP_FAMILY: + return index, (step_name, component, uses_target, registered.is_multivariate) + if step_name in _STEP_FAMILY and legacy_step is None: family, component = _STEP_FAMILY[step_name] - return family, component, False, False - return "passthrough", "raw", False, False + legacy_step = index, (family, component, False, False) + return legacy_step if legacy_step is not None else (None, ("passthrough", "raw", False, False)) + + +def _resolve_block_representation(pipeline, columns): + """Return ``(family, component, uses_target, is_interaction)`` for a block.""" + return _locate_block_representation(pipeline, columns)[1] def block_uses_target(transformer) -> bool: @@ -408,6 +421,38 @@ def block_uses_target(transformer) -> bool: return bool(_resolve_block_representation(representation, None)[2]) +def refit_block_representation(transformer, X, y) -> TransformerLike: + """Return a copy of a fitted per-column block whose representation is refit on ``(X, y)``. + + Only the representation step and the steps after it are refit. The steps + before it (the imputer with its missing indicator, scalers) never use the + target, so they are kept from the all-data fit and transform ``X`` for the + refit: a ``SimpleImputer(add_indicator=True)`` refit on rows without missing + values would drop its indicator column and so change the block's width. For + a separate-state union the missing branch is kept as well. The fitted + ``transformer`` itself is left untouched. + """ + separate_state = _separate_state_branches(transformer) + if separate_state is not None: + representation = refit_block_representation(separate_state[0], X, y) + union = copy.copy(transformer) + union.transformer_list = [ + (name, representation if name == "representation" else branch) + for name, branch in transformer.transformer_list + ] + return union + index = _locate_block_representation(transformer, None)[0] + if not isinstance(transformer, Pipeline) or index is None or index == 0: + refit = cast(TransformerLike, clone(transformer)) + refit.fit(X, y) + return refit + refit = cast(Pipeline, clone(transformer[index:])) + refit.fit(transformer[:index].transform(X), y) + block = copy.copy(transformer) + block.steps = [*transformer.steps[:index], *refit.steps] + return cast(TransformerLike, block) + + def _passthrough_source(columns, offset, feature_names_in): """Resolve the source feature name for a passthrough / remainder column.""" column = columns[offset] if offset < len(columns) else columns[-1] diff --git a/pretab/core/supervised.py b/pretab/core/supervised.py index 44158be..0f8eb61 100644 --- a/pretab/core/supervised.py +++ b/pretab/core/supervised.py @@ -125,12 +125,14 @@ class CrossFittedTransformer(RepresentationSpecMixin, TransformerMixin, BaseEsti transformer : estimator A supervised (target-aware) PreTab transformer to cross-fit, or an estimator such as a :class:`~pretab.Preprocessor` or ``Pipeline`` built - from them. For a :class:`~pretab.Preprocessor`, only its target-aware blocks are refit per - fold: column types and the blocks that do not use ``y``, including their - category vocabularies, come from the all-data fit, so their out-of-fold features use exactly the - encoding of :meth:`transform`. Any other estimator is cloned and refit + from them. For a :class:`~pretab.Preprocessor`, only its target-aware + representations and subsequent steps are refit per fold. Column types, + target-free blocks and their vocabularies, imputers, scalers before the + representation and missing-indicator branches come from the all-data fit. + Target-free feature values match :meth:`transform`; target-aware values + exclude the held-out targets. Any other estimator is cloned and refit from scratch on each fold. A pandas DataFrame ``X`` is passed through - unchanged (each fold is a row subset), so column names, dtypes and index labels are preserved. Polars + unchanged (each fold is a row subset), preserving column names, dtypes and index labels. Polars DataFrames are converted once to pandas before selecting fold rows. n_folds : int, default=5 Number of cross-fitting folds. Must be at least 2. @@ -227,8 +229,9 @@ def fit_transform(self, X, y=None): raise IncompatibleParamsError( "Cross-fitting requires a fixed output width across folds; expected " f"{width}, got {fold_out.shape[1]}. The wrapped transformer's width " - "depends on the training rows (e.g. adaptive sizing); fix it, for " - "example by disabling adaptive sizing." + "depends on the rows it is fit on (e.g. adaptive sizing, or category / " + "indicator columns learned from the data); configure it to produce the " + "same columns on every fold." ) out[test_idx] = fold_out finally: diff --git a/pretab/preprocessor.py b/pretab/preprocessor.py index 170dccc..bc25b9e 100644 --- a/pretab/preprocessor.py +++ b/pretab/preprocessor.py @@ -30,6 +30,7 @@ build_transformer_summary, feature_names_out, get_output_slices, + refit_block_representation, representation_leaf, ) from .compose.output import ( @@ -1118,16 +1119,20 @@ def estimate_memory(self, X) -> int: return int(n_rows * n_cols * self._output_itemsize()) def _cross_fit_fold(self, X, y): - """Return a copy of this fit whose target-aware blocks are refit on ``(X, y)``. + """Return a copy of this fit whose target-aware representations are refit on ``(X, y)``. Used by :class:`~pretab.CrossFittedTransformer` for its out-of-fold - features. Only the blocks that consume the target are refit on the fold; - column types and target-free blocks, including their category vocabularies, are shared - with this all-data fit, so an out-of-fold row is encoded exactly like + features. Only the representations that consume the target are refit on + the fold. Column types, target-free blocks and their vocabularies, and + preprocessing before the representation (imputers and scalers) are + shared with this all-data fit. Missing-indicator branches are shared too. + An out-of-fold row is encoded exactly like ``transform`` encodes it, except that no target-aware placement has seen that row's target. Refitting the whole preprocessor per fold would re-detect column types and relearn categories on fewer rows, shifting - integer codes and one-hot columns between the folds and ``transform``. + integer codes and one-hot columns between the folds and ``transform``; + refitting a block's missing indicator would drop its column on a fold + without missing values. """ check_is_fitted(self) # Each block sees the fold rows exactly as the internal ColumnTransformer @@ -1138,8 +1143,7 @@ def _cross_fit_fold(self, X, y): for name, transformer, columns in self.column_transformer_.transformers_: fold_transformer = transformer if name != "remainder" and block_uses_target(transformer): - fold_transformer = cast(TransformerLike, clone(transformer)) - fold_transformer.fit(X_ct[list(columns)], y) + fold_transformer = refit_block_representation(transformer, X_ct[list(columns)], y) span = self.column_transformer_.output_indices_[name] expected_width = span.stop - span.start width = len(fold_transformer.get_feature_names_out(list(columns))) @@ -1147,7 +1151,7 @@ def _cross_fit_fold(self, X, y): raise IncompatibleParamsError( f"Cross-fitting requires a fixed output width for block {block_name(name, columns)!r}; " f"expected {expected_width}, got {width}. The block's width depends on the training rows; " - "use a fixed-width representation and stable missing-indicator columns." + "configure the representation to produce the same columns on every fold." ) column_transformer.transformers_.append((name, fold_transformer, columns)) fold = copy.copy(self) diff --git a/tests/core/test_cross_fitted.py b/tests/core/test_cross_fitted.py index a26bfbb..86cf2ab 100644 --- a/tests/core/test_cross_fitted.py +++ b/tests/core/test_cross_fitted.py @@ -13,6 +13,7 @@ from sklearn.model_selection import KFold from pretab import CrossFittedTransformer, LeakageWarning +from pretab.core._typing import TransformerLike from pretab.exceptions import IncompatibleParamsError, InvalidParamError from pretab.transformers import PLETransformer @@ -146,10 +147,33 @@ def test_wrapped_preprocessor_out_of_fold_codes_match_transform(array_with_rare_ np.testing.assert_array_equal(np.unique(out[:, unsupervised[1]]), [1.0, 2.0, 3.0, 4.0]) +def _refit_representation_per_fold(pipeline, column, y): + """Expected out-of-fold rows of a fitted block pipeline whose last step is its + target-aware representation: the helper steps before it keep their all-data fit.""" + from sklearn.base import clone + + helpers, representation = pipeline[:-1], pipeline[-1] + blocks = {} + for train_idx, test_idx in KFold(n_splits=5, shuffle=True, random_state=0).split(column): + fold = cast(TransformerLike, clone(representation)) + fold.fit(helpers.transform(column.iloc[train_idx]), y[train_idx]) + blocks[tuple(test_idx)] = fold.transform(helpers.transform(column.iloc[test_idx])) + out = np.empty((len(column), next(iter(blocks.values())).shape[1])) + for test_idx, rows in blocks.items(): + out[list(test_idx)] = rows + return out + + +def _fitted_block(cross_fitted, name): + from pretab import Preprocessor + + return cast(Preprocessor, cross_fitted.estimator_).column_transformer_.named_transformers_[name] + + def test_wrapped_preprocessor_target_aware_blocks_stay_out_of_fold(array_with_rare_category): - """Only the target-aware block is refit, on each fold's training rows.""" + """Only the target-aware representation is refit, on each fold's training rows; the + helper steps before it (imputer, scaler) keep their all-data fit.""" import pandas as pd - from sklearn.base import clone from pretab import Preprocessor @@ -159,12 +183,10 @@ def test_wrapped_preprocessor_target_aware_blocks_stay_out_of_fold(array_with_ra supervised = _split_columns(cross_fitted, "num_feature_0_ple") assert len(supervised) == 4 - wrapped = cast(Preprocessor, cross_fitted.estimator_) - block = wrapped.column_transformer_.named_transformers_["num_feature_0"] - column = pd.DataFrame({"feature_0": X[:, 0]}) - for train_idx, test_idx in KFold(n_splits=5, shuffle=True, random_state=0).split(X): - fold_block = clone(block).fit(column.iloc[train_idx], y[train_idx]) - np.testing.assert_allclose(out[np.ix_(test_idx, supervised)], fold_block.transform(column.iloc[test_idx])) + block = _fitted_block(cross_fitted, "num_feature_0") + assert [name for name, _ in block.steps] == ["imputer", "minmax", "ple"] + expected = _refit_representation_per_fold(block, pd.DataFrame({"feature_0": X[:, 0]}), y) + np.testing.assert_array_equal(out[:, supervised], expected) assert not np.allclose(out[:, supervised], np.asarray(cross_fitted.transform(X))[:, supervised]) @@ -260,7 +282,7 @@ def test_wrapped_preprocessor_reads_the_target_use_of_an_indicator_block(): assert not np.allclose(out, full) # refit on every fold -def test_width_mismatch_message_does_not_blame_adaptive_sizing_only(): +def test_width_mismatch_names_the_general_cause(): from sklearn.preprocessing import OneHotEncoder x = np.array(["a"] * 99 + ["b"]).reshape(-1, 1) # the fold holding out "b" never sees it @@ -268,10 +290,116 @@ def test_width_mismatch_message_does_not_blame_adaptive_sizing_only(): cross_fitted = CrossFittedTransformer( OneHotEncoder(handle_unknown="ignore", sparse_output=False), n_folds=5, random_state=0 ) - with pytest.raises(IncompatibleParamsError, match="depends on the training rows"): + with pytest.raises(IncompatibleParamsError, match="same columns on every fold"): cross_fitted.fit_transform(x, y) +# --- a wrapped Preprocessor shares its missing indicators across folds (issue #89) -- + + +@pytest.fixture +def array_with_one_missing_value(): + rng = np.random.default_rng(0) + X = rng.normal(size=(200, 2)) + X[7, 0] = np.nan # the fold holding out row 7 trains on no missing value + y = np.nan_to_num(X[:, 0]) + rng.normal(scale=0.1, size=200) + return X, y + + +@pytest.mark.parametrize( + ("method", "missing"), + [ + ("ple", {"add_missing_indicator": True}), + ("ple", {"missing_policy": "impute_with_indicator"}), + ("rbf", {"add_missing_indicator": True}), + ("relu", {"add_missing_indicator": True}), + ("sigmoid", {"add_missing_indicator": True}), + ("tanh", {"add_missing_indicator": True}), + ], +) +def test_wrapped_preprocessor_keeps_its_missing_indicator_columns(array_with_one_missing_value, method, missing): + """Each fold refit the block's imputer, whose missing indicator emits no column when + the fold's training rows contain no missing value, so the width check failed.""" + from pretab import Preprocessor + + X, y = array_with_one_missing_value + + def cross_fit(**missing_config): + wrapped = Preprocessor( + numerical_method=method, output_dim=4, output_format="dense", random_state=0, **missing_config + ) + cross_fitted = CrossFittedTransformer(wrapped, n_folds=5, random_state=0) + return cross_fitted, cross_fitted.fit_transform(X, y) + + with_indicator, out = cross_fit(**missing) + _, imputed_only = cross_fit(missing_policy="impute") + names = [str(name) for name in with_indicator.get_feature_names_out()] + indicator = [i for i, name in enumerate(names) if "missingindicator" in name] + + assert out.shape == np.asarray(with_indicator.transform(X)).shape + assert indicator # the indicator columns of the all-data fit are kept on every fold + # The value's out-of-fold representation is the one cross-fitting gives without an indicator. + np.testing.assert_array_equal(np.delete(out, indicator, axis=1), imputed_only) + + +def test_wrapped_preprocessor_refits_only_the_representation_after_the_imputer(array_with_one_missing_value): + """The all-data imputer and scaler feed only the value to the fold's PLE; + the raw missing indicator remains alongside the representation.""" + import pandas as pd + + from pretab import Preprocessor + + X, y = array_with_one_missing_value + wrapped = Preprocessor( + numerical_method="ple", output_dim=4, add_missing_indicator=True, output_format="dense", random_state=0 + ) + cross_fitted = CrossFittedTransformer(wrapped, n_folds=5, random_state=0) + out = cross_fitted.fit_transform(X, y) + + block = _fitted_block(cross_fitted, "num_feature_0") + representation = dict(block.transformer_list)["representation"] + assert [name for name, _ in representation.steps] == ["imputer", "minmax", "ple"] + columns = _split_columns(cross_fitted, "num_feature_0") + assert len(columns) == 5 + assert out.shape == (len(X), 9) + expected = _refit_representation_per_fold(representation, pd.DataFrame({"feature_0": X[:, 0]}), y) + np.testing.assert_array_equal(out[:, columns], np.column_stack((expected, np.isnan(X[:, 0]).astype(float)))) + + +@pytest.mark.parametrize("method", ["bspline", "mspline", "ispline", "cubicspline", "naturalspline"]) +def test_wrapped_preprocessor_keeps_the_indicator_next_to_a_target_aware_spline(array_with_one_missing_value, method): + from pretab import Preprocessor + + X, y = array_with_one_missing_value + wrapped = Preprocessor(numerical_method=method, add_missing_indicator=True, output_format="dense", random_state=0) + cross_fitted = CrossFittedTransformer(wrapped, n_folds=5, random_state=0) + out = cross_fitted.fit_transform(X, y) + assert out.shape == cross_fitted.transform(X).shape + indicator = [i for i, name in enumerate(cross_fitted.get_feature_names_out()) if "missingindicator" in name] + assert len(indicator) == 1 + np.testing.assert_array_equal(out[:, indicator[0]], np.isnan(X[:, 0]).astype(float)) + + +def test_wrapped_preprocessor_keeps_the_separate_state_missing_branch(array_with_one_missing_value): + """The representation uses the all-data imputer and scaler, while its raw missing state is shared.""" + import pandas as pd + + from pretab import Preprocessor + + X, y = array_with_one_missing_value + wrapped = Preprocessor( + numerical_method="ple", output_dim=4, missing_policy="separate_state", output_format="dense", random_state=0 + ) + cross_fitted = CrossFittedTransformer(wrapped, n_folds=5, random_state=0) + out = cross_fitted.fit_transform(X, y) + + representation = dict(_fitted_block(cross_fitted, "num_feature_0").transformer_list)["representation"] + expected = _refit_representation_per_fold(representation, pd.DataFrame({"feature_0": X[:, 0]}), y) + columns = _split_columns(cross_fitted, "num_feature_0") + assert len(columns) == 5 + np.testing.assert_array_equal(out[:, columns], np.column_stack((expected, np.isnan(X[:, 0]).astype(float)))) + + # --- DataFrame input is passed through (issue #58) -------------------------------- @@ -430,7 +558,8 @@ def record_cross_fit_fold(self, X, y): @pytest.mark.parametrize("method", ["ple", "bspline", "mspline", "ispline", "rbf"]) @pytest.mark.parametrize("task", ["regression", "classification"]) -def test_cross_fitting_lightgbm_refits_only_target_blocks_and_keeps_the_full_fit(method, task): +@pytest.mark.parametrize("add_missing_indicator", [False, True]) +def test_cross_fitting_lightgbm_refits_only_target_blocks_and_keeps_the_full_fit(method, task, add_missing_indicator): pytest.importorskip("lightgbm") import pandas as pd from sklearn.base import clone @@ -442,6 +571,8 @@ def test_cross_fitting_lightgbm_refits_only_target_blocks_and_keeps_the_full_fit y = 2 * X[1].to_numpy() + X[2].to_numpy() + rng.normal(scale=0.1, size=len(X)) if task == "classification": y = np.where(y >= np.median(y), "high", "low") + if add_missing_indicator: + X.loc[7, 1] = np.nan preprocessor = Preprocessor( numerical_method=method, categorical_method="one-hot", @@ -449,12 +580,14 @@ def test_cross_fitting_lightgbm_refits_only_target_blocks_and_keeps_the_full_fit output_dim=6, output_format="dense", task=task, + add_missing_indicator=add_missing_indicator, random_state=0, ) cross_fitted = CrossFittedTransformer(preprocessor, n_folds=3, task=task, random_state=0) out = cross_fitted.fit_transform(X, y) full = cross_fitted.transform(X) - assert out.shape == full.shape == (len(X), 8) + width = 8 + int(add_missing_indicator) + assert out.shape == full.shape == (len(X), width) assert not np.allclose(out[:, :6], full[:, :6]) np.testing.assert_array_equal(out[:, 6:], full[:, 6:]) reference = clone(preprocessor) @@ -462,10 +595,10 @@ def test_cross_fitting_lightgbm_refits_only_target_blocks_and_keeps_the_full_fit reference_out = reference.fit(X, y).transform(X) assert isinstance(reference_out, np.ndarray) np.testing.assert_allclose(full, reference_out) - assert len(cross_fitted.get_feature_names_out()) == 8 + assert len(cross_fitted.get_feature_names_out()) == width fitted = cross_fitted.estimator_ assert isinstance(fitted, Preprocessor) - assert [item.uses_target for item in fitted.get_feature_lineage()] == [True] * 6 + [False] * 2 + assert [item.uses_target for item in fitted.get_feature_lineage()] == [True] * 6 + [False] * (width - 6) @pytest.mark.parametrize("labels", [[1, 2], [np.int64(7), np.int64(9)], [1, "flag"], ["amount", "flag"]]) diff --git a/tests/extension/test_registered_cross_fitting.py b/tests/extension/test_registered_cross_fitting.py index c520052..399aa9a 100644 --- a/tests/extension/test_registered_cross_fitting.py +++ b/tests/extension/test_registered_cross_fitting.py @@ -12,11 +12,14 @@ import numpy as np import pandas as pd import pytest -from sklearn.base import BaseEstimator, TransformerMixin -from sklearn.model_selection import KFold -from sklearn.preprocessing import TargetEncoder +from sklearn.base import BaseEstimator, TransformerMixin, clone +from sklearn.impute import MissingIndicator, SimpleImputer +from sklearn.model_selection import KFold, StratifiedKFold +from sklearn.pipeline import FeatureUnion, Pipeline +from sklearn.preprocessing import MinMaxScaler, TargetEncoder from pretab import CrossFittedTransformer, Preprocessor, register_representation +from pretab.compose.inspection import block_uses_target, refit_block_representation from pretab.exceptions import IncompatibleParamsError @@ -92,6 +95,27 @@ def test_registered_target_encoder_is_refit_per_fold(noise_target_data, cls, sup assert lineage["cat_id"] is True +def test_registered_target_encoder_keeps_its_missing_indicator(): + """The registered encoder is refit on the imputed value; its raw indicator stays alongside it.""" + rng = np.random.default_rng(0) + X = rng.choice(["a", "b", "c"], (200, 1)).astype(object) + X[7, 0] = np.nan + y = rng.normal(size=200) + register_representation("registered_encoder", TargetEncoder, feature_kind="categorical", supervision="supervised") + pre = Preprocessor( + categorical_method="registered_encoder", add_missing_indicator=True, output_format="dense", random_state=0 + ) + + cross_fitted = CrossFittedTransformer(pre, n_folds=5, random_state=0) + out = cross_fitted.fit_transform(X, y) + full = cross_fitted.transform(X) + + assert list(cross_fitted.get_feature_names_out()) == ["cat_feature_0", "cat_feature_0__missingindicator_feature_0"] + assert out.shape == full.shape == (200, 2) + assert not np.allclose(out, full) # refit on every fold + np.testing.assert_array_equal(out[:, 1], pd.isna(X[:, 0]).astype(float)) + + @pytest.mark.parametrize("target_aware", [False, True]) @pytest.mark.parametrize("boolean_input", [False, True]) def test_optional_class_follows_the_preprocessor_target_aware_across_folds( @@ -177,3 +201,119 @@ def test_opposite_block_width_changes_are_rejected_even_when_total_width_is_fixe # Full-data widths are (2, 2); fold widths (3, 1) keep the same total but move the feature boundary. with pytest.raises(IncompatibleParamsError, match=r"block 'num_a'.*expected 2, got 3"): cross_fitted.fit_transform(X, y) + + +@pytest.mark.parametrize("leading_helpers", [False, True]) +@pytest.mark.parametrize("trailing_scaler", ["none", "suffix", "scaler"]) +@pytest.mark.parametrize("missing_union", [False, True]) +def test_refit_representation_shares_only_the_prefix_and_missing_branch( + leading_helpers, trailing_scaler, missing_union +): + register_representation("pipeline_mean", _MeanEncoder, feature_kind="numerical", supervision="optional") + X = pd.DataFrame({"x": np.repeat(np.arange(20, dtype=float), 10)}) + y = np.random.default_rng(0).normal(size=len(X)) + steps = [] + if leading_helpers: + X.loc[7, "x"] = np.nan + steps.extend([("imputer", SimpleImputer()), ("scale", MinMaxScaler())]) + prefix_length = len(steps) + steps.append(("pipeline_mean", _MeanEncoder())) + if trailing_scaler != "none": + steps.append((trailing_scaler, MinMaxScaler())) + pipeline = Pipeline(steps) + fitted = ( + FeatureUnion([("representation", pipeline), ("missing", MissingIndicator(features="all"))]) + if missing_union + else pipeline + ).fit(X, y) + assert block_uses_target(fitted) + original = fitted.transform(X).copy() + original_input = X.copy(deep=True) + train, held_out = X.iloc[40:], X.iloc[:40] + refitted = refit_block_representation(fitted, train, y[40:]) + assert refitted is not fitted + if missing_union: + assert isinstance(fitted, FeatureUnion) + assert isinstance(refitted, FeatureUnion) + old_branches = dict(fitted.transformer_list) + new_branches = dict(refitted.transformer_list) + assert new_branches["missing"] is old_branches["missing"] + new_pipeline = new_branches["representation"] + np.testing.assert_array_equal(refitted.transform(held_out)[:, -1], pd.isna(held_out["x"]).astype(float)) + else: + new_pipeline = refitted + assert isinstance(new_pipeline, Pipeline) + for index, (_, step) in enumerate(pipeline.steps): + assert (new_pipeline.steps[index][1] is step) is (index < prefix_length) + + prefix = pipeline[:prefix_length] if prefix_length else None + prepared_train = prefix.transform(train) if prefix is not None else train + prepared_test = prefix.transform(held_out) if prefix is not None else held_out + expected = clone(pipeline[prefix_length:]) + assert isinstance(expected, Pipeline) + expected.fit(prepared_train, y[40:]) + np.testing.assert_allclose(new_pipeline.transform(held_out), expected.transform(prepared_test)) + np.testing.assert_array_equal(fitted.transform(X), original) + pd.testing.assert_frame_equal(X, original_input) + pd.testing.assert_frame_equal(train, original_input.iloc[40:]) + + +def test_refit_a_bare_encoder_does_not_change_its_all_data_fit(noise_target_data): + X, y = noise_target_data + values = X[["id"]] + fitted = _MeanEncoder().fit(values, y) + original = fitted.transform(values).copy() + refitted = refit_block_representation(fitted, values.iloc[80:], y[80:]) + assert isinstance(refitted, _MeanEncoder) + assert refitted is not fitted + expected = _MeanEncoder().fit(values.iloc[80:], y[80:]).transform(values.iloc[:80]) + np.testing.assert_array_equal(refitted.transform(values.iloc[:80]), expected) + np.testing.assert_array_equal(fitted.transform(values), original) + + +@pytest.mark.parametrize("target_kind", ["continuous", "binary", "multiclass"]) +def test_named_boolean_target_encoder_keeps_its_indicator_and_excludes_held_out_targets(target_kind): + rng = np.random.default_rng(0) + X = pd.DataFrame({"flag": rng.choice([True, False], size=200).astype(object)}, index=np.arange(200) + 1000) + X.iloc[7, 0] = np.nan + original = X.copy(deep=True) + if target_kind == "continuous": + y = rng.normal(size=len(X)) + task = "regression" + splitter = KFold(5, shuffle=True, random_state=0) + else: + classes = np.array(["A", "B"] if target_kind == "binary" else ["A", "B", "C"]) + y = classes[np.arange(len(X)) % len(classes)] + task = "classification" + splitter = StratifiedKFold(5, shuffle=True, random_state=0) + register_representation("registered_encoder", TargetEncoder, feature_kind="categorical", supervision="supervised") + cross_fitted = CrossFittedTransformer( + Preprocessor( + categorical_method="registered_encoder", + add_missing_indicator=True, + output_format="dense", + task=task, + random_state=0, + ), + n_folds=5, + task=task, + random_state=0, + ) + out = cross_fitted.fit_transform(X, y) + width = 3 if target_kind == "multiclass" else 1 + assert out.shape == cross_fitted.transform(X).shape == (len(X), width + 1) + fitted = cross_fitted.estimator_ + assert isinstance(fitted, Preprocessor) + branches = dict(fitted.column_transformer_.named_transformers_["cat_flag"].transformer_list) + pipeline = branches["representation"] + prepared = pipeline[:-1].transform(X) + expected = np.empty((len(X), width)) + for train_idx, test_idx in splitter.split(X, y): + encoder = clone(pipeline[-1]) + assert isinstance(encoder, TargetEncoder) + encoder.fit(prepared[train_idx], y[train_idx]) + expected[test_idx] = encoder.transform(prepared[test_idx]) + np.testing.assert_allclose(out[:, :width], expected) + np.testing.assert_array_equal(out[:, -1], pd.isna(X["flag"]).astype(float)) + assert [item.uses_target for item in fitted.get_feature_lineage()] == [True] * width + [False] + pd.testing.assert_frame_equal(X, original) diff --git a/tests/integration/test_polars_input.py b/tests/integration/test_polars_input.py index 64e86b3..0e27e55 100644 --- a/tests/integration/test_polars_input.py +++ b/tests/integration/test_polars_input.py @@ -251,3 +251,32 @@ def cross_fit(X): np.testing.assert_allclose(from_polars.transform(polars_frame), from_pandas.transform(pandas_frame)) np.testing.assert_array_equal(from_polars.get_feature_names_out(), from_pandas.get_feature_names_out()) assert from_polars.get_representation_spec().input_features == ("x", "kids") + + +@pytest.mark.parametrize("method", ["ple", "bspline", "rbf"]) +@pytest.mark.parametrize("policy", ["impute_with_indicator", "separate_state"]) +def test_cross_fitting_keeps_single_missing_value_indicators_for_polars(method, policy): + from pretab import CrossFittedTransformer + + rng = np.random.default_rng(0) + X = rng.normal(size=200) + X[7] = np.nan + pandas_frame = pd.DataFrame({"x": X}) + polars_frame = pl.DataFrame({"x": [None if np.isnan(value) else float(value) for value in X]}) + y = rng.normal(size=len(X)) + + def cross_fit(frame): + wrapped = CrossFittedTransformer( + Preprocessor(numerical_method=method, missing_policy=policy, output_format="dense", random_state=0), + n_folds=5, + random_state=0, + ) + return wrapped, wrapped.fit_transform(frame, y) + + reference, expected = cross_fit(pandas_frame) + wrapped, actual = cross_fit(polars_frame) + assert actual.shape == wrapped.transform(polars_frame).shape == (len(X), 8) + np.testing.assert_allclose(actual, expected) + np.testing.assert_array_equal(actual[:, -1], np.isnan(X).astype(float)) + np.testing.assert_array_equal(wrapped.get_feature_names_out(), reference.get_feature_names_out()) + assert polars_frame.null_count()["x"][0] == 1