From d7bf5432ca24e27df6181708bc38b5f60bb03bbe Mon Sep 17 00:00:00 2001 From: ChrisW09 <50968720+ChrisW09@users.noreply.github.com> Date: Fri, 16 Oct 2026 14:25:11 +0200 Subject: [PATCH 1/2] fix(cross-fitting): return transform's output type from fit_transform CrossFittedTransformer.fit_transform wrote the out-of-fold rows into a preallocated float64 buffer, so it disagreed with transform: a wrapped Preprocessor(categorical_method=None) raised on its unchanged string columns, sparse output (output_format="sparse") raised because the fold width could not be read, dtype=np.float32 gave float64, and a DataFrame-returning estimator gave an ndarray. The fold outputs are now collected and stacked in the original row order with their own container: sparse blocks are stacked with scipy.sparse.vstack in the transform format, pandas / polars DataFrames are concatenated (keeping the input index when the blocks carry it, renumbered otherwise), and dense blocks take their common dtype. The width check still runs on every fold, a DataFrame fold whose columns differ from the all-data fit raises instead of being misaligned by label, and a dict of blocks (output_structure="blocks") raises a clear error instead of an IndexError. Fixes #90 --- docs/core_concepts/target_awareness.md | 4 +- pretab/core/supervised.py | 89 ++++++++++++-- tests/core/test_cross_fitted.py | 156 +++++++++++++++++++++++++ 3 files changed, 241 insertions(+), 8 deletions(-) diff --git a/docs/core_concepts/target_awareness.md b/docs/core_concepts/target_awareness.md index d3eccf2..b1f0ab4 100644 --- a/docs/core_concepts/target_awareness.md +++ b/docs/core_concepts/target_awareness.md @@ -70,7 +70,9 @@ this for the training features specifically. It splits the training data into fo fresh copy of the transformer on all folds _except_ one, and uses that copy to transform the held-out fold, so every training row is transformed by a model that never saw its own target. `transform` on genuinely new data (a validation or test set) instead uses one model fit on all -the training data, since there is no leakage risk there. +the training data, since there is no leakage risk there. Both return the same kind of output, +the one the wrapped transformer produces: a dense array of the same dtype, a sparse matrix in +the same format, or a DataFrame. ```python import numpy as np diff --git a/pretab/core/supervised.py b/pretab/core/supervised.py index f6aca3b..7940383 100644 --- a/pretab/core/supervised.py +++ b/pretab/core/supervised.py @@ -16,9 +16,11 @@ import sys import warnings from dataclasses import replace -from typing import cast +from typing import Any, cast import numpy as np +import pandas as pd +from scipy import sparse as sp from sklearn.base import BaseEstimator, TransformerMixin, clone from sklearn.model_selection import KFold, StratifiedKFold from sklearn.utils.validation import check_is_fitted @@ -87,6 +89,40 @@ def warn_target_leakage(estimator, y) -> None: ) +def _stack_folds(blocks, test_indices, X) -> Any: + """Stack the out-of-fold ``blocks`` and put their rows back in the order of ``X``. + + ``blocks[i]`` holds the transformed rows ``test_indices[i]``. The result keeps + the kind of output the wrapped transformer returns: sparse blocks stay sparse + (in the format of the first block), pandas / polars DataFrames keep their + columns and dtypes, and dense blocks take their common dtype (``object`` for + strings left unchanged). A pandas DataFrame keeps the index of ``X`` when the + blocks carry it (scikit-learn's ``set_output`` copies the input index); + otherwise it is renumbered from 0, as ``transform`` numbers the rows of a + transformer that builds its own index (such as a Preprocessor). + """ + rows = np.concatenate(test_indices) + # order[i] is the position of input row i among the stacked fold rows. + order = np.empty_like(rows) + order[rows] = np.arange(len(rows)) + first = blocks[0] + if all(sp.issparse(block) for block in blocks): + return sp.vstack(blocks).tocsr()[order].asformat(first.format) + if all(isinstance(block, pd.DataFrame) for block in blocks): + stacked = pd.concat(blocks).iloc[order] + index = getattr(X, "index", None) + return stacked if index is not None and stacked.index.equals(index) else stacked.reset_index(drop=True) + if type(first).__module__.partition(".")[0] == "polars": + import polars as pl # type: ignore + + return pl.concat(blocks)[order] + dense = [block.toarray() if sp.issparse(block) else np.asarray(block) for block in blocks] + out = np.empty((len(rows), first.shape[1]), dtype=np.result_type(*dense)) + for block, test_idx in zip(dense, test_indices, strict=True): + out[test_idx] = block + return out + + class CrossFittedTransformer(RepresentationSpecMixin, TransformerMixin, BaseEstimator): """Cross-fit a supervised transformer to remove target leakage on training data. @@ -94,6 +130,8 @@ class CrossFittedTransformer(RepresentationSpecMixin, TransformerMixin, BaseEsti training fold and used to transform the held-out fold, so every training row is encoded by a model that never saw its own target. :meth:`transform` (for unseen data) uses ``estimator_``, a single transformer fit on all data. + Both return the wrapped transformer's kind of output: a dense array of the + same dtype, a sparse matrix in the same format, or a DataFrame. Parameters ---------- @@ -171,27 +209,64 @@ def transform(self, X): return self.estimator_.transform(X_arr) def fit_transform(self, X, y=None): - """Fit and return leakage-free out-of-fold features for the training data.""" + """Fit and return leakage-free out-of-fold features for the training data. + + Returns + ------- + array-like of shape (n_samples, n_features_out) + The out-of-fold features, in the row order of ``X`` and in the kind of + output :meth:`transform` returns: a sparse matrix in the same format, a + DataFrame, or a dense array with the folds' dtype (``object`` when the + wrapped transformer leaves strings unchanged). + + Raises + ------ + IncompatibleParamsError + If the wrapped transformer returns a dict of blocks (which cannot be + stacked), or if a fold's output has a different width, or DataFrame + columns other than the all-data fit's. + """ X_arr, y_arr = self._fit_full(X, y) - width = len(self.estimator_.get_feature_names_out()) - out = np.empty((X_arr.shape[0], width), dtype=float) + names = [str(name) for name in self.estimator_.get_feature_names_out()] + width = len(names) + blocks, test_indices = [], [] splitter = self._make_splitter() token = _cross_fit_active.set(True) 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_out = fold.transform(X_arr[test_idx]) + if isinstance(fold_out, dict): + raise IncompatibleParamsError( + "Cross-fitting stacks the out-of-fold features into one array; the " + "wrapped transformer returned a dict of blocks. Use output_structure=" + "'matrix' on a wrapped Preprocessor." + ) + if not hasattr(fold_out, "shape"): + fold_out = np.asarray(fold_out) if fold_out.shape[1] != width: raise IncompatibleParamsError( "Cross-fitting requires a fixed output width across folds; expected " f"{width}, got {fold_out.shape[1]}. Disable adaptive sizing on the " "wrapped transformer." ) - out[test_idx] = fold_out + # DataFrames are stacked by column label, so a fold that kept other + # columns at the same width (e.g. other top categories) cannot be aligned. + columns = getattr(fold_out, "columns", None) + if columns is not None and [str(column) for column in columns] != names: + differing = sorted(set(map(str, columns)).symmetric_difference(names)) + raise IncompatibleParamsError( + "Cross-fitting requires the same output columns across folds; a fold's " + f"columns differ from the all-data fit in {differing}. The wrapped " + "transformer learns its columns from the rows it is fit on; configure it " + "to produce the same columns on every fold." + ) + blocks.append(fold_out) + test_indices.append(test_idx) finally: _cross_fit_active.reset(token) - return out + return _stack_folds(blocks, test_indices, X_arr) def get_feature_names_out(self, input_features=None): """Delegate output feature names to the all-data ``estimator_``.""" diff --git a/tests/core/test_cross_fitted.py b/tests/core/test_cross_fitted.py index 723d971..a72a595 100644 --- a/tests/core/test_cross_fitted.py +++ b/tests/core/test_cross_fitted.py @@ -104,3 +104,159 @@ def test_invalid_task_rejected(data): X, y = data with pytest.raises(InvalidParamError, match="task"): CrossFittedTransformer(PLETransformer(), task="classificaton").fit_transform(X, y) + + +# --- fit_transform returns the kind of output transform returns (issue #90) --------- + + +@pytest.fixture +def integer_array(): + """A numerical and a categorical integer column whose types and categories every fold sees.""" + rng = np.random.default_rng(0) + X = np.column_stack([rng.integers(0, 100_000, 300), rng.choice([1, 2, 3], 300)]) + y = (X[:, 0] / 50_000.0) ** 2 + (X[:, 1] == 2) + rng.normal(scale=0.1, size=300) + return X, y + + +@pytest.fixture +def object_array(): + """A float and a string column in one object array.""" + rng = np.random.default_rng(0) + X = np.empty((300, 2), dtype=object) + X[:, 0] = rng.normal(size=300) + X[:, 1] = rng.choice(["Berlin", "Paris", "Rome"], 300) + return X, X[:, 0].astype(float) ** 2 + rng.normal(scale=0.1, size=300) + + +def _cross_fit(wrapped, X, y): + cross_fitted = CrossFittedTransformer(wrapped, n_folds=3, random_state=0) + return cross_fitted, cross_fitted.fit_transform(X, y) + + +def test_fit_transform_keeps_strings_left_unchanged(object_array): + """The out-of-fold rows were written into a float buffer, so categorical_method=None + (strings passed through unchanged) raised in fit_transform but not in transform.""" + from pretab import Preprocessor + + X, y = object_array + cross_fitted, out = _cross_fit(Preprocessor(categorical_method=None, random_state=0), X, y) + + assert out.dtype == cross_fitted.transform(X).dtype == object + np.testing.assert_array_equal(out, X) + + +def test_fit_transform_stacks_out_of_fold_values_with_strings(object_array): + from sklearn.compose import ColumnTransformer + + X, y = object_array + wrapped = ColumnTransformer([("ple", PLETransformer(output_dim=4), [0]), ("keep", "passthrough", [1])]) + _, out = _cross_fit(wrapped, X, y) + _, numeric = _cross_fit(PLETransformer(output_dim=4), X[:, [0]].astype(float), y) + + assert out.dtype == object + np.testing.assert_allclose(out[:, :4].astype(float), numeric) + np.testing.assert_array_equal(out[:, 4], X[:, 1]) + + +def test_fit_transform_keeps_sparse_output(integer_array): + """fit_transform could not stack sparse fold output, while transform returned CSR.""" + from scipy import sparse as sp + + from pretab import Preprocessor + + X, y = integer_array + params = {"categorical_method": "one-hot", "output_dim": 4, "random_state": 0} + cross_fitted, out = _cross_fit(Preprocessor(output_format="sparse", **params), X, y) + _, dense = _cross_fit(Preprocessor(**params), X, y) + + assert sp.issparse(out) + assert out.format == cross_fitted.transform(X).format == "csr" + np.testing.assert_allclose(out.toarray(), dense) + + +def test_fit_transform_keeps_the_configured_dtype(integer_array): + from pretab import Preprocessor + + X, y = integer_array + cross_fitted, out = _cross_fit(Preprocessor(dtype=np.float32, output_dim=4, random_state=0), X, y) + _, native = _cross_fit(Preprocessor(output_dim=4, random_state=0), X, y) + + assert out.dtype == cross_fitted.transform(X).dtype == np.float32 + np.testing.assert_allclose(out, native, rtol=1e-6) + + +def test_fit_transform_returns_the_dataframe_transform_returns(integer_array): + import pandas as pd + + from pretab import Preprocessor + + X, y = integer_array + cross_fitted, out = _cross_fit(Preprocessor(output_dim=4, random_state=0).set_output(transform="pandas"), X, y) + full = cross_fitted.transform(X) + _, array = _cross_fit(Preprocessor(output_dim=4, random_state=0), X, y) + + assert isinstance(out, pd.DataFrame) + pd.testing.assert_index_equal(out.columns, full.columns) + pd.testing.assert_index_equal(out.index, full.index) # the Preprocessor numbers its rows + np.testing.assert_allclose(out.to_numpy(), array) + + +def test_fit_transform_returns_polars_like_transform(integer_array): + pl = pytest.importorskip("polars") + from pretab import Preprocessor + + X, y = integer_array + cross_fitted, out = _cross_fit(Preprocessor(output_dim=4, random_state=0).set_output(transform="polars"), X, y) + _, array = _cross_fit(Preprocessor(output_dim=4, random_state=0), X, y) + + assert isinstance(out, pl.DataFrame) + assert out.columns == cross_fitted.transform(X).columns + np.testing.assert_allclose(out.to_numpy(), array) + + +def test_stacked_dataframe_folds_keep_the_input_index_they_carry(): + """scikit-learn's set_output copies the input index onto each fold's frame.""" + import pandas as pd + + from pretab.core.supervised import _stack_folds + + X = pd.DataFrame({"a": [10.0, 11.0, 12.0, 13.0]}, index=pd.Index([7, 3, 9, 1])) + test_indices = [np.array([1, 3]), np.array([0, 2])] + blocks = [X.iloc[rows] * 2 for rows in test_indices] + + pd.testing.assert_frame_equal(_stack_folds(blocks, test_indices, X), X * 2) + renumbered = _stack_folds([block.reset_index(drop=True) for block in blocks], test_indices, X) + pd.testing.assert_frame_equal(renumbered, (X * 2).reset_index(drop=True)) + + +def test_fit_transform_rejects_folds_with_other_dataframe_columns(): + """Frames are stacked by column label: a fold that keeps other columns at the + same width (here another top category) must raise, not widen the result with + NaN-padded columns.""" + from sklearn.compose import ColumnTransformer + from sklearn.preprocessing import OneHotEncoder + + # Unshuffled folds: fold 0 holds out rows 0-29 (all "a"), so its training rows keep + # "b" as the frequent category while the all-data fit keeps "a" -- same width, other labels. + X = np.empty((90, 2), dtype=object) + X[:, 0] = np.linspace(-1.0, 1.0, 90) + X[:, 1] = ["a"] * 30 + ["b"] * 30 + ["c"] * 20 + ["a"] * 10 + y = X[:, 0].astype(float) ** 2 + encoder = OneHotEncoder(max_categories=2, handle_unknown="infrequent_if_exist", sparse_output=False) + transformer = ColumnTransformer([("ple", PLETransformer(output_dim=3), [0]), ("oh", encoder, [1])]).set_output( + transform="pandas" + ) + + with warnings.catch_warnings(): + warnings.simplefilter("ignore", LeakageWarning) + with pytest.raises(IncompatibleParamsError, match=r"same output columns across folds.*x1_a"): + CrossFittedTransformer(transformer, n_folds=3, shuffle=False).fit_transform(X, y) + + +def test_cross_fitting_rejects_blocks(integer_array): + from pretab import Preprocessor + + X, y = integer_array + blocks = Preprocessor(output_structure="blocks", random_state=0) + with pytest.raises(IncompatibleParamsError, match="dict of blocks"): + CrossFittedTransformer(blocks, n_folds=3, random_state=0).fit_transform(X, y) From 93b76315d5951dac63c50fcf96854389b81e92f1 Mon Sep 17 00:00:00 2001 From: Manish Kumar Date: Mon, 2 Nov 2026 05:34:04 +0100 Subject: [PATCH 2/2] fix(cross-fitting): preserve sparse types on minimum SciPy Keep sparse matrix and array classes through SciPy 1.10 stacking. Check the concrete output type without relying on the newer scipy.sparse.sparray export. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- pretab/core/supervised.py | 3 ++- tests/core/test_cross_fitted.py | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/pretab/core/supervised.py b/pretab/core/supervised.py index 714da6b..2adb49b 100644 --- a/pretab/core/supervised.py +++ b/pretab/core/supervised.py @@ -109,7 +109,8 @@ def _stack_folds(blocks, test_indices, X) -> Any: order[rows] = np.arange(len(rows)) first = blocks[0] if all(sp.issparse(block) for block in blocks): - return sp.vstack(blocks).tocsr()[order].asformat(first.format) + # Older SciPy vstack returns a matrix even when its inputs are sparse arrays. + return type(first)(sp.vstack(blocks).tocsr()[order]) if all(isinstance(block, pd.DataFrame) for block in blocks): stacked = pd.concat(blocks).iloc[order] index = getattr(X, "index", None) diff --git a/tests/core/test_cross_fitted.py b/tests/core/test_cross_fitted.py index cfe86dd..85b8087 100644 --- a/tests/core/test_cross_fitted.py +++ b/tests/core/test_cross_fitted.py @@ -330,7 +330,7 @@ def reject_densification(*args, **kwargs): out = _stack_folds(blocks, rows, values) assert sp.issparse(out) assert out.format == sparse_format - assert isinstance(out, sp.sparray) is sparse_array + assert type(out) is type(blocks[0]) assert out.dtype == np.float32 np.testing.assert_array_equal(out.toarray(), values)