Skip to content

FIX: Fix QuantileTransformer(ignore_implicit_zeros=True) sub-sampling behavior - #32587

Merged
lesteve merged 12 commits into
scikit-learn:mainfrom
cakedev0:fix/quantile_tr_sparse_ignore_zeros
Sep 7, 2026
Merged

lesteve merged 12 commits into
scikit-learn:mainfrom
cakedev0:fix/quantile_tr_sparse_ignore_zeros

Conversation

@cakedev0

@cakedev0 cakedev0 commented Oct 27, 2025 •

Copy link
Copy Markdown
Member

Reference Issues/PRs

Fixes: #32585

What does this implement/fix? Explain your changes.

Fix the logic for the sub-sampling (only affect the case ignore_implicit_zeros=True)

Adds a non-regression test.

Any other comments?

While reviewing the PR, you might notice "behavior discontinuity" that this PR fixes also exists for the case ignore_implicit_zeros=False. But for this case, it doesn't incur unexpected/incorrect results, just unexpectedly long/short runtime. See issue #34298 about that.

I haven't written a change-log as nobody reported the bug except me, and I found it just by reading the code... Let me know if this is needed.

@github-actions

github-actions Bot commented Oct 27, 2025 •

Copy link
Copy Markdown

✔️ Linting Passed

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

Generated for commit: 326d0bf. Link to the linter CI: here

@cakedev0
cakedev0 marked this pull request as ready for review October 27, 2025 19:34
@cakedev0 cakedev0 changed the title [WIP] FIX: Fix QuantileTransformer(ignore_implicit_zeros=True) sub-sampling behavior FIX: Fix QuantileTransformer(ignore_implicit_zeros=True) sub-sampling behavior Oct 27, 2025
@betatim

betatim commented Oct 29, 2025

Copy link
Copy Markdown
Member

I'd add a change log entry, even if no one has reported a bug people might notice a change in behaviour. The entry is the way we want people to answer that kind of question for themselves without posting here.

Fix looks good to me. Not thought about the test yet.

mdshoaibuddinchanda pushed a commit to mdshoaibuddinchanda/scikit-learn that referenced this pull request Nov 2, 2025
…bug (scikit-learn#32587)

Add deterministic regression test that constructs a very sparse CSC matrix with two nearly-identical columns (one with subsample - 1 nonzeros, the other with subsample + 1 nonzeros) and verifies that the transformed columns are not degenerate after fit/transform.

For large declared n_samples, the bug could create a zero-sized column sample leading to collapsed quantiles (all zeros) for some columns.

References: scikit-learn#32587
mdshoaibuddinchanda pushed a commit to mdshoaibuddinchanda/scikit-learn that referenced this pull request Nov 2, 2025
…eros=True

When ignore_implicit_zeros=True, use subsample directly instead of scaling by n_samples to avoid zero-sized column samples. Also add max(1) guard for the else case.

Fixes scikit-learn#32587
mdshoaibuddinchanda pushed a commit to mdshoaibuddinchanda/scikit-learn that referenced this pull request Nov 2, 2025
@cakedev0

Copy link
Copy Markdown
Member Author

Ping @snath-xoc, I was told you're working on QuantileTransformer currently, so you might be interested by this PR and the corresponding issues.

@snath-xoc snath-xoc left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for this @cakedev0 , overall looks solid, I suggested one change in the tests to make sure we do not have fallback to the degenerate case that this PR should be fixing. Overall looks good otherwise and good spot!

@snath-xoc

Copy link
Copy Markdown
Contributor

While reviewing the PR, you might notice "behavior discontinuity" that this PR fixes also exists for the case ignore_implicit_zeros=False. But for this case, it doesn't incur unexpected/incorrect results, just unexpectedly long/short runtime. See issue [TODO] about that.

P.S. which did you manage to open the issue [TODO] yet?

@snath-xoc snath-xoc added this to Labs Jun 12, 2026
@snath-xoc snath-xoc moved this to In progress in Labs Jun 12, 2026
@github-actions github-actions Bot added the CI:Linter failure The linter CI is failing on this PR label Jun 15, 2026
@cakedev0

Copy link
Copy Markdown
Member Author

I had not created the TODO issue yet, I just did it: #34298

@github-actions github-actions Bot removed the CI:Linter failure The linter CI is failing on this PR label Jun 17, 2026
@ogrisel ogrisel moved this from In progress to PR waiting for reviews in Labs Jun 29, 2026
Comment thread sklearn/preprocessing/_data.py Resolved

@snath-xoc snath-xoc left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @cakedev0 for this, I suggested some more documentation under _sparse_fit so that subsampling behaviour changes is noted. Not sure if that is the right place, feel free to move it around.

Otherwise LGTM I suppose we would need some follow-up PRs to look more closely at #34298 for the ignore_implicit_zeros=False and len(column_nnz_data) <= subsample case?

@glemaitre glemaitre left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks good. Just a couple of nitpicks.

Comment thread sklearn/preprocessing/_data.py Resolved
@cakedev0

cakedev0 commented Sep 7, 2026

Copy link
Copy Markdown
Member Author

@glemaitre I think we can merge this?

@lesteve
lesteve merged commit cfb2977 into scikit-learn:main Sep 7, 2026
32 checks passed
@github-project-automation github-project-automation Bot moved this from PR waiting for reviews to Done in Labs Sep 7, 2026
@jeremiedbb jeremiedbb mentioned this pull request Sep 8, 2026
14 tasks done
jeremiedbb pushed a commit to jeremiedbb/scikit-learn that referenced this pull request Sep 8, 2026
jeremiedbb pushed a commit to jeremiedbb/scikit-learn that referenced this pull request Sep 8, 2026
jeremiedbb pushed a commit to jeremiedbb/scikit-learn that referenced this pull request Sep 8, 2026
jeremiedbb pushed a commit to jeremiedbb/scikit-learn that referenced this pull request Sep 9, 2026
jeremiedbb pushed a commit to jeremiedbb/scikit-learn that referenced this pull request Sep 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: QuantileTransformer(ignore_implicit_zeros=True) sub-sampling behavior is incorrect

5 participants