Skip to content

BUG fix sparse QuantileTransformer subsampling - #35274

Open
rushabh-borkar wants to merge 2 commits into
scikit-learn:mainfrom
rushabh-borkar:fix-sparse-quantile-subsampling
Open

rushabh-borkar wants to merge 2 commits into
scikit-learn:mainfrom
rushabh-borkar:fix-sparse-quantile-subsampling

Conversation

@rushabh-borkar

@rushabh-borkar rushabh-borkar commented Oct 30, 2026 •

Copy link
Copy Markdown

Reference Issues/PRs

Fixes #34857.

What does this implement/fix? Explain your changes.

This PR fixes sparse QuantileTransformer subsampling when ignore_implicit_zeros=False.

Previously, the implementation scaled the number of sampled non-zero values by the column density. For a very sparse feature, subsample=10_000 could therefore retain only a small number of non-zero observations. The resulting quantiles were noisy and could distort the transformed distribution.

The implementation now samples up to subsample non-zero values directly and uses weighted percentiles to represent the exact number of implicit zeros. This preserves the zero mass without materializing an array proportional to the number of input rows. Existing behavior is preserved for ignore_implicit_zeros=True and for columns that do not require subsampling.

A regression test uses a very sparse column with 100,000 rows and verifies that the subsampled quantiles remain close to the full-data quantiles. The test fails with the previous density-scaled sampling logic.

A changelog entry was added at doc/whats_new/upcoming_changes/sklearn.preprocessing/35274.fix.rst.

Validation:

  • git diff --check — passed.
  • python -m compileall -q sklearn\\preprocessing\\_data.py sklearn\\preprocessing\\tests\\test_data.py — passed.
  • Standalone weighted-percentile validation — passed; the new calculation is within the regression tolerance and the previous algorithm is not.
  • GitHub CI: all required checks passed, including lint, CodeQL, documentation, analysis, changelog, and unit-test checks. Eight optional checks were skipped.
  • Local targeted pytest execution was blocked because the scikit-learn C extension was not built.
  • Local editable build was blocked by the available MSVC compiler, which could not compile the extension target.

Introduce yourself

This contribution addresses the sparse QuantileTransformer subsampling bug reported in #34857.

AI usage disclosure

I used AI assistance for:

  • Code generation (e.g., when writing an implementation or fixing a bug)
  • Test/benchmark generation
  • Documentation (including examples)
  • Research and understanding

Any other comments?

The fix is intentionally limited to the sparse subsampling path and keeps the existing full-materialization behavior for non-subsampled sparse columns.

This pull request includes code written with the assistance of AI.
The code has not yet been reviewed by a human.

@github-actions

Copy link
Copy Markdown

Thank you for opening your first pull request to scikit-learn! 🎉

To help get your contribution reviewed, please make sure that:

  • You have filled out the pull request template.

  • The pull request addresses an existing issue that is ready for contribution (e.g. not tagged as 'Needs Triage', 'Needs Decision', ...). If you are proposing a new feature, please open an issue to discuss it first.

  • There are no other open pull requests already targeting the same issue.

  • You have followed the pull request checklist. In particular, linting and tests should pass.

@rushabh-borkar rushabh-borkar changed the title BUG improve sparse QuantileTransformer subsampling BUG fix sparse QuantileTransformer subsampling Oct 30, 2026

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: QuantileTransformer() sub-sampling impact is too high for sparse matrices

1 participant