Repository navigation
FIX SimpleImputer.inverse_transform column order with all-NaN features - #33792
RudrenduPaul wants to merge 4 commits into
Conversation
…it-learn#33792) Built by Rudrendu Paul, developed with Claude Code
|
Added changelog entry at |
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
…it-learn#33792) 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.
ae57274 to
e136b4e
Compare
|
⏰ 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 |
Reference Issue
Fixes #27012.
What does this implement / fix?
When
SimpleImputeris fitted withadd_indicator=Trueon data that has one or more all-NaN features (andkeep_empty_features=False, the default), thetransformoutput drops those features from the imputed part while still appending indicator columns for them.The previous
inverse_transformimplementation 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_maskfromnp.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 tomissing_valuesafter 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:inverse_transformreturns the original number of columns.missing_values(NaN).The test fails on
mainand passes with this fix.Built by Rudrendu Paul, developed with Claude Code