From 981423405ca877711c922d44d3a4f36b51f858c8 Mon Sep 17 00:00:00 2001 From: ChrisW09 <50968720+ChrisW09@users.noreply.github.com> Date: Fri, 16 Oct 2026 14:12:41 +0200 Subject: [PATCH] fix(cross-fitting): pass DataFrames to the wrapped transformer unchanged CrossFittedTransformer converted X with np.asarray and sliced folds from that array, so a mixed-dtype DataFrame reached the wrapped estimator as an object array without column names. A wrapped Preprocessor then treated every column as categorical (every out-of-fold value of a numeric feature became the 'unseen category' code), and name-based estimators such as a ColumnTransformer selecting columns by label raised. DataFrames are now passed through unchanged, with each fold taken as a row subset by position; other input is still converted to a 2D array. Fixes #58 --- pretab/core/supervised.py | 37 ++++++++++++++----- tests/core/test_cross_fitted.py | 65 +++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+), 10 deletions(-) diff --git a/pretab/core/supervised.py b/pretab/core/supervised.py index f6aca3b..a6f0531 100644 --- a/pretab/core/supervised.py +++ b/pretab/core/supervised.py @@ -87,6 +87,25 @@ def warn_target_leakage(estimator, y) -> None: ) +def _as_2d(X): + """Return ``X`` as 2D input for the wrapped transformer. + + A pandas DataFrame is passed through unchanged, so column names and per-column + dtypes reach the wrapped estimator (e.g. a Preprocessor detecting numerical + vs categorical columns, or a name-based ColumnTransformer); any other input + is converted to a NumPy array, with 1D input reshaped to a single column. + """ + if hasattr(X, "iloc") and getattr(X, "ndim", None) == 2: + return X + X = np.asarray(X) + return X.reshape(-1, 1) if X.ndim == 1 else X + + +def _take_rows(X, indices): + """Select rows of a DataFrame or array by position.""" + return X.iloc[indices] if hasattr(X, "iloc") else X[indices] + + class CrossFittedTransformer(RepresentationSpecMixin, TransformerMixin, BaseEstimator): """Cross-fit a supervised transformer to remove target leakage on training data. @@ -98,7 +117,10 @@ class CrossFittedTransformer(RepresentationSpecMixin, TransformerMixin, BaseEsti Parameters ---------- transformer : estimator - A supervised (target-aware) PreTab transformer to cross-fit. + A supervised (target-aware) PreTab transformer to cross-fit, or an + estimator such as a :class:`~pretab.Preprocessor` or ``Pipeline`` built from + them. A pandas DataFrame ``X`` is passed to it unchanged (each fold is a row + subset), so column names and dtypes are preserved. n_folds : int, default=5 Number of cross-fitting folds. Must be at least 2. task : {"regression", "classification"}, default="regression" @@ -141,9 +163,7 @@ def _fit_full(self, X, y): raise InvalidParamError(f"n_folds must be an integer >= 2; got {self.n_folds!r}.") if self.task not in ("regression", "classification"): raise InvalidParamError(f"task must be 'regression' or 'classification'; got {self.task!r}.") - X_arr = np.asarray(X) - if X_arr.ndim == 1: - X_arr = X_arr.reshape(-1, 1) + X_arr = _as_2d(X) y_arr = np.asarray(y).ravel() if len(X_arr) != len(y_arr): raise PretabDataError(f"X and y must have same length. Got {len(X_arr)} and {len(y_arr)}") @@ -165,10 +185,7 @@ def fit(self, X, y=None): def transform(self, X): """Transform ``X`` using the transformer fit on all training data.""" check_is_fitted(self, "estimator_") - X_arr = np.asarray(X) - if X_arr.ndim == 1: - X_arr = X_arr.reshape(-1, 1) - return self.estimator_.transform(X_arr) + return self.estimator_.transform(_as_2d(X)) def fit_transform(self, X, y=None): """Fit and return leakage-free out-of-fold features for the training data.""" @@ -180,8 +197,8 @@ def fit_transform(self, X, y=None): try: for train_idx, test_idx in splitter.split(X_arr, y_arr): fold = cast(TransformerLike, clone(self.transformer)) - fold.fit(X_arr[train_idx], y_arr[train_idx]) - fold_out = np.asarray(fold.transform(X_arr[test_idx])) + fold.fit(_take_rows(X_arr, train_idx), y_arr[train_idx]) + fold_out = np.asarray(fold.transform(_take_rows(X_arr, test_idx))) if fold_out.shape[1] != width: raise IncompatibleParamsError( "Cross-fitting requires a fixed output width across folds; expected " diff --git a/tests/core/test_cross_fitted.py b/tests/core/test_cross_fitted.py index 723d971..c0dc73a 100644 --- a/tests/core/test_cross_fitted.py +++ b/tests/core/test_cross_fitted.py @@ -6,6 +6,7 @@ """ import warnings +from typing import cast import numpy as np import pytest @@ -104,3 +105,67 @@ def test_invalid_task_rejected(data): X, y = data with pytest.raises(InvalidParamError, match="task"): CrossFittedTransformer(PLETransformer(), task="classificaton").fit_transform(X, y) + + +# --- DataFrame input is passed through (issue #58) -------------------------------- + + +@pytest.fixture +def mixed_frame(): + import pandas as pd + + rng = np.random.default_rng(0) + X = pd.DataFrame( + {"num": rng.normal(size=300), "city": rng.choice(["a", "b", "c"], size=300)}, + index=rng.permutation(1000)[:300], + ) + return X, X["num"].to_numpy() ** 2 + rng.normal(scale=0.1, size=300) + + +def test_wrapped_preprocessor_keeps_dataframe_column_types(mixed_frame): + """Regression guard for issue #58: np.asarray turned the frame into an object + array, so the wrapped Preprocessor treated every column as categorical.""" + from pretab import Preprocessor + + X, y = mixed_frame + direct = Preprocessor(output_dim=6, random_state=0).fit(X, y) + cross_fitted = CrossFittedTransformer(Preprocessor(output_dim=6, random_state=0), n_folds=3, random_state=0) + out = cross_fitted.fit_transform(X, y) + + wrapped = cast(Preprocessor, cross_fitted.estimator_) + assert wrapped.numerical_features_ == ["num"] + assert wrapped.categorical_features_ == ["city"] + assert out.shape == (300, direct.total_output_dim_) + assert list(cross_fitted.get_feature_names_out()) == list(direct.get_feature_names_out()) + assert len(np.unique(out[:, 0])) > 1 # a supervised encoding, not an "unseen category" code + assert cross_fitted.transform(X).shape == out.shape + + +def test_wrapped_column_transformer_selects_by_column_name(mixed_frame): + from sklearn.compose import ColumnTransformer + + X, y = mixed_frame + wrapped = ColumnTransformer([("ple", PLETransformer(output_dim=4), ["num"])]) + out = CrossFittedTransformer(wrapped, n_folds=3, random_state=0).fit_transform(X, y) + assert out.shape == (300, 4) + + +def test_wrapped_preprocessor_accepts_feature_preprocessing_by_column_name(mixed_frame): + from pretab import Preprocessor + + X, y = mixed_frame + wrapped = Preprocessor( + feature_preprocessing={"num": "ple"}, numerical_method="minmax", output_dim=4, random_state=0 + ) + cross_fitted = CrossFittedTransformer(wrapped, n_folds=3, random_state=0) + out = cross_fitted.fit_transform(X, y) + + assert list(cross_fitted.get_feature_names_out()) == [f"num_num_ple{i}" for i in range(4)] + ["cat_city"] + assert out.shape == (300, 5) + + +def test_one_dimensional_input_is_still_a_single_column(): + rng = np.random.default_rng(0) + x = rng.normal(size=200) + out = CrossFittedTransformer(PLETransformer(output_dim=4), n_folds=3, random_state=0).fit_transform(x, x**2) + assert out.shape == (200, 4)