Skip to content

FIX avoid densifying sparse input in QuantileTransformer._sparse_fit - #34451

Open
Stag51 wants to merge 9 commits into
scikit-learn:mainfrom
Stag51:fix-quantile-sparse-34298
Open

Stag51 wants to merge 9 commits into
scikit-learn:mainfrom
Stag51:fix-quantile-sparse-34298

Conversation

@Stag51

@Stag51 Stag51 commented Jul 9, 2026 •

Copy link
Copy Markdown

Reference Issues/PRs

Fixes #34298

What does this implement/fix? Explain your changes.

QuantileTransformer._sparse_fit materialized a dense array of size
n_samples when computing percentiles for a sparse column with
ignore_implicit_zeros=False, even when the column had very few
non-zero entries. For very large, very sparse matrices this caused a
MemoryError (or unnecessary slowdown for moderately sparse cases).

This PR adds _sparse_column_percentile, which computes percentiles
directly from the non-zero values plus a count of implicit zeros,
without allocating an n_samples-length array. Since sparse input to
QuantileTransformer is required to be non-negative, implicit zeros
are guaranteed to occupy the lowest ranks when sorted, which makes an
analytical rank-based computation possible.

Changes:

  • Added _sparse_column_percentile() as a module-level helper.
  • Updated _sparse_fit() to use it for the non-subsampled,
    ignore_implicit_zeros=False code path (the one that previously
    triggered the bug).
  • Added a non-regression test covering the original OOM scenario.
  • Added a changelog entry.

Testing:

  • Verified the fix produces numerically identical output to the
    original dense-densifying approach on cases where the original
    approach is still tractable (np.allclose comparison).
  • Ran the full QuantileTransformer/QuantileTransform test suite in
    sklearn/preprocessing/tests/test_data.py locally — all passing.

First time contributor introduction

This is my first contribution to scikit-learn. I'm Saad, an AI/ML
engineer based in Lahore, working mostly on RAG pipelines, agentic
systems, and backend ML infra day-to-day. I've been meaning to get
into open-source ML contributions for a while and picked this issue
because it was a self-contained, well-scoped numerical bug rather than
something touching the Cython internals — figured it was a reasonable
first PR to actually understand the codebase properly rather than
just skimming it. Looking forward to contributing more.

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?

I reproduced the original bug locally before starting, and validated
the fix against the original (memory-hungry but correct) computation
to confirm numerical equivalence before touching the actual
_sparse_fit code. Along the way I introduced a regression in the
subsampled branch (it stopped appending to self.quantiles_), which
test_quantile_transform_subsampling caught immediately — good
reminder of why that test exists. Happy to adjust the approach if
there's a preferred direction, or add more test coverage if reviewers
want it.

@github-actions

github-actions Bot commented Jul 9, 2026

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.

cakedev0 added a commit to cakedev0/scikit-learn that referenced this pull request Aug 31, 2026
Credits Saad Shabbir who independently proposed the same fix in scikit-learn#34451,
which this PR supersedes.

Co-authored-by: Saad Shabbir <[email protected]>
cakedev0 added a commit to cakedev0/scikit-learn that referenced this pull request Aug 31, 2026
Credits Saad Shabbir who independently proposed the same fix in scikit-learn#34451,
which this PR supersedes.

Co-authored-by: Saad Shabbir <[email protected]>
cakedev0 added a commit to cakedev0/scikit-learn that referenced this pull request Aug 31, 2026
Credits Saad Shabbir who independently proposed the same fix in scikit-learn#34451,
which this PR supersedes.

Co-authored-by: Saad Shabbir <[email protected]>

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() may materialize sparse matrices

1 participant