Skip to content

FIX SimpleImputer.inverse_transform column order with all-NaN features - #33792

Open
RudrenduPaul wants to merge 4 commits into
scikit-learn:mainfrom
RudrenduPaul:fix/simple-imputer-inverse-transform-empty-feature
Open

RudrenduPaul wants to merge 4 commits into
scikit-learn:mainfrom
RudrenduPaul:fix/simple-imputer-inverse-transform-empty-feature

Conversation

@RudrenduPaul

@RudrenduPaul RudrenduPaul commented Apr 19, 2026 •

Copy link
Copy Markdown
Contributor

Reference Issue

Fixes #27012.

What does this implement / fix?

When SimpleImputer is fitted with add_indicator=True on data that has one or more all-NaN features (and keep_empty_features=False, the default), the transform output drops those features from the imputed part while still appending indicator columns for them.

The previous inverse_transform implementation was unaware of this drop. Its column-mapping loop assumed every original feature had a corresponding imputed column, so it assigned imputed values to the wrong positions when an all-NaN feature sat before a valid feature. The result was silently wrong column order and spurious values.

Root cause: the loop used np.all(X_original[:, col]) (all indicator bits set) to detect and skip dropped features. This works when all rows are NaN in the new data, but fails when the new data has non-NaN values in the previously-empty column (indicator is 0, condition is False, loop mistakenly assigns an imputed value there).

Fix: compute invalid_mask from np.isnan(self.statistics_) before the loop. Features that were entirely NaN during fit are flagged unconditionally. The loop skips these columns (does not consume an imputed value), and they are set to missing_values after the loop.

Test Plan

Added test_simple_imputation_inverse_transform_empty_feature — a parametrized regression test that places the all-NaN column at the first, middle, and last position and verifies:

  1. inverse_transform returns the original number of columns.
  2. The reconstructed all-NaN column contains only missing_values (NaN).
  3. All other columns round-trip correctly.

The test fails on main and passes with this fix.

Built by Rudrendu Paul, developed with Claude Code


AI Assistance Disclosure (required per AGENTS.md)
This pull request includes code written with the assistance of AI (Claude Code).
All changes have been read, understood, and verified by the human contributor (Rudrendu Paul).

RudrenduPaul added a commit to RudrenduPaul/scikit-learn that referenced this pull request Apr 19, 2026
@RudrenduPaul

Copy link
Copy Markdown
Contributor Author

Added changelog entry at doc/whats_new/upcoming_changes/sklearn.impute/33792.fix.rst to resolve the Check Changelog CI failure.

CCA (mode B) uses pseudo-inverse computations which become numerically
unstable when n_features > n_samples or n_targets > n_samples — the
fitted weights may depend on column order and should not be interpreted.
This note was already present as a code comment (line 73-74) but was
missing from the public docstring visible to users.

Adds a Notes section to the CCA class docstring documenting the
instability condition and suggesting PLSCanonical as an alternative,
with a reference to the Wegelin review already cited in the source.

Fixes scikit-learn#19042

Built by Rudrendu Paul, developed with Claude Code
…icator

When keep_empty_features=False (the default) and add_indicator=True, any
feature that is entirely NaN during fit is dropped from the transform output.
The previous inverse_transform implementation did not account for these
"empty" features: it mapped imputed columns to the wrong original positions,
producing shifted column order and silently incorrect results.

Fix: detect all-NaN features via NaN-check on statistics_ before the
column-mapping loop, skip them (don't consume an imputed column), and
restore them to missing_values at the end.

Added parametrized regression tests covering the empty feature at the first,
middle, and last column positions.

Closes scikit-learn#27012

Built by Rudrendu Paul, developed with Claude Code
…er.inverse_transform

Closes the codecov/patch gap on the all-NaN feature fix: the
keep_empty_features=True short-circuit (invalid_mask = np.zeros(...))
was never exercised by the existing tests.
@RudrenduPaul
RudrenduPaul force-pushed the fix/simple-imputer-inverse-transform-empty-feature branch from ae57274 to e136b4e Compare August 9, 2026 06:06
@lorentzenchr lorentzenchr added the autoclose PR automatically closed 14 days after setting the label label Nov 15, 2026
@github-actions

Copy link
Copy Markdown

⏰ This pull request might be automatically closed in two weeks from now.

Thank you for your contribution to scikit-learn and for the effort you have put into this PR. This pull request does not yet meet the quality and clarity needed for an effective review. Project maintainers have limited time for code reviews, and our goal is to prioritize well-prepared contributions to keep scikit-learn maintainable.

To increase the chance of a productive review, please refer to: How do I improve my issue or pull request? As the author, you are responsible for driving this PR, which entails doing necessary background research as well as presenting its context and your thought process. If you are a new contributor, or do not know how to fulfill these requirements, we recommend that you familiarise yourself with scikit-learn's development conventions via other contribution types (e.g., reviewing PRs) before submitting code.

Scikit-learn maintainers cannot provide one-to-one guidance on this PR. However, if you ask focused, well-researched questions, a community member may be willing to help. 💬

If you substantially improve this PR within two weeks, a team member may remove the autoclose label and the PR stays open. Cosmetic changes or incomplete fixes will not be sufficient. Maintainers will assess improvements on their own schedule. Please do not ping (@) maintainers.

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

autoclose PR automatically closed 14 days after setting the label module:cross_decomposition module:impute

Projects

None yet

Development

Successfully merging this pull request may close these issues.

inverse_transform of SimpleImputer with empty features changes order of columns

2 participants