Skip to content

[MRG] Fix simple imputer with empty features doesn't put back removed columns - #28729

Closed
miguelcsilva wants to merge 7 commits into
scikit-learn:mainfrom
miguelcsilva:fix-simple-imputer-with-empty-features-doesnt-put-back-removed-columns
Closed

miguelcsilva wants to merge 7 commits into
scikit-learn:mainfrom
miguelcsilva:fix-simple-imputer-with-empty-features-doesnt-put-back-removed-columns

Conversation

@miguelcsilva

@miguelcsilva miguelcsilva commented Mar 30, 2024 •

Copy link
Copy Markdown
Contributor

Reference Issues/PRs

Fixes #27012

What does this implement/fix? Explain your changes.

Even though the discussion #27012 (comment) suggested two separate PRs I found it easier to make a single one since the two issues were tightly related.

The following changes were made:

  • SimpleImputer.fit saves self._removals_mask and self._removals if self.keep_empty_features is False to keep track of the indices where removals were made and of the arrays that were actually removed.
  • SimpleImputer.inverse_transform was refactored to replace the while loop with numpy vectorized operations.
  • SimpleImputer.inverse_transform was expanded to handle the case of empty features having been remove and now puts them back in the original positions.
  • test_simple_imputation_inverse_transform was refactored to use pytest.mark.parametrize. The original test cases were kept and 3 more test cases added to address the case where the entire columns have missing values.

Any other comments?

To avoid the transform and inverse_transform methods diverging in the future, I would suggest that in a separate PR, the transform function is changed to start using the new self._removals_mask and self._removals attributes to decide on the features to remove. I'll be happy to work on a PR for that if this one is approved.

I have not made any changes to the documentation since I considered very unlikely someone was actually relying on the bug. But I'm happy to make changes to the docs if you consider helpful.

@github-actions

github-actions Bot commented Mar 30, 2024 •

Copy link
Copy Markdown

✔️ Linting Passed

All linting checks passed. Your pull request is in excellent shape! ☀️

Generated for commit: 468ff0b. Link to the linter CI: here

@lorentzenchr

Copy link
Copy Markdown
Member

@Micky774 ping for potential review.

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

Projects

Status: In Progress

Development

Successfully merging this pull request may close these issues.

inverse_transform of SimpleImputer with empty features changes order of columns

2 participants