Skip to content

fix(cross-fitting): cross-fit estimators that cannot name their output - #164

Open
ChrisW09 wants to merge 1 commit into
mainfrom
fix/163-crossfit-without-feature-names
Open

ChrisW09 wants to merge 1 commit into
mainfrom
fix/163-crossfit-without-feature-names

Conversation

@ChrisW09

Copy link
Copy Markdown
Collaborator

Fixes #163.

CrossFittedTransformer.fit_transform raised AttributeError for a wrapped estimator that cannot name its output columns, such as a Pipeline or ColumnTransformer with a FunctionTransformer step, although fit().transform() worked.

Root cause

fit_transform (pretab/core/supervised.py) began with names = list(self.estimator_.get_feature_names_out()). The names were used only to know the expected fold width and to compare DataFrame fold columns, but the call had no fallback.

Fix

  • A new _all_data_feature_names() returns the all-data fit's names, or None when get_feature_names_out raises AttributeError. This is how scikit-learn's Pipeline and ColumnTransformer signal a step without names.
  • Without names, the first fold's width is the expected width, and for DataFrame output its columns become the reference labels. Every later fold is checked against them with the existing errors. The messages then say "the first fold" instead of "the all-data fit".
  • With names, the behaviour is unchanged.

Behaviour changes

  • Wrapped estimators without output names can be cross-fitted. Before, fit_transform raised AttributeError.
  • get_feature_names_out() on the wrapper still delegates to the wrapped estimator, so it still raises for such an estimator, as scikit-learn does.

Documentation

  • CrossFittedTransformer.fit_transform docstring (Raises): the first fold is the reference when the wrapped transformer provides no get_feature_names_out.
  • docs/core_concepts/target_awareness.md and docs/api/search_and_cross_fitting.rst (fold contract): the same fallback.
  • README, homepage and tutorials do not cover this case.

Tests

In tests/core/test_cross_fitted.py:

  • test_fit_transform_works_without_output_feature_names: Pipeline(FunctionTransformer(np.log1p), PLETransformer) matches a manual out-of-fold computation with the same splitter. Fails on main, passes here.
  • test_column_transformer_without_output_feature_names_is_cross_fitted: a mixed ColumnTransformer. Fails on main, passes here.
  • test_fold_width_is_still_checked_without_output_feature_names: a transformer without names whose width depends on its training rows still raises the fixed-width IncompatibleParamsError. Fails on main (with AttributeError), passes here.

Reproduction

The script from #163 on this branch:

Pipeline(log1p, PLE) fit().transform(): (200, 10)
Pipeline(log1p, PLE) fit_transform(): (200, 10)
ColumnTransformer(PLE, log) fit().transform(): (200, 6)
ColumnTransformer(PLE, log) fit_transform(): (200, 6)

Checks

  • just test: 4679 passed, 59 skipped, 7 xfailed; coverage 95.69% (gate 90%).
  • Minimum dependencies (CI job: Python 3.11, numpy 1.24.4, pandas 2.0.3, scipy 1.10.1, scikit-learn 1.6.0): 4496 passed, 173 skipped, 7 xfailed; scripts/quickstart.py passes.
  • just docs: strict build succeeds.
  • just check: all pre-commit and pre-push hooks pass (file hygiene, ruff format, ruff lint, prettier, pyright with 0 errors).
  • The change stays inside pretab.core.supervised; no import direction changes.

Merge notes

fit_transform read the all-data fit's get_feature_names_out() only to
learn the expected fold width and DataFrame columns, so a Pipeline or
ColumnTransformer with a FunctionTransformer step, or any transformer
without the method, raised AttributeError although fit().transform()
worked.

When the wrapped estimator cannot provide names, the first fold's width
and columns are now the reference, and every later fold is still checked
against them.

Fixes #163

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CrossFittedTransformer.fit_transform crashes when the wrapped estimator has no get_feature_names_out

1 participant