Skip to content

FIX Avoid densifying sparse columns in QuantileTransformer/RobustScaler quantile computation - #34858

Open
cakedev0 wants to merge 27 commits into
scikit-learn:mainfrom
cakedev0:fix/quantile_tr_sparse_no_materialize
Open

cakedev0 wants to merge 27 commits into
scikit-learn:mainfrom
cakedev0:fix/quantile_tr_sparse_no_materialize

Conversation

@cakedev0

@cakedev0 cakedev0 commented Aug 31, 2026 •

Copy link
Copy Markdown
Member

Reference Issues/PRs

Fixes #34298. Fixes #34857.

Supersedes #34451, which proposes the same _sparse_column_percentile-style fix for #34298 but without the part fixing #34857. Credited @Stag51 as co-author on the commit implementing it and in the changelog.

What does this implement/fix? Explain your changes.

Two related bugs in how QuantileTransformer (and RobustScaler) compute quantiles from sparse columns:

To fix #34298, I added _sparse_column_quantile, which computes nanquantile-equivalent results directly from the non-zero data plus an implicit-zero count, without ever densifying the column.

I also ported the same fix to RobustScaler, which had the identical densify-then-nanpercentile pattern.

For #34857, the subsampling path now draws up to self.subsample non-zero values directly and subsamples the implicit-zero count proportionally instead => The amount of real signal retained no longer depends on how sparse the column is relative to n_samples. But the calculation volume is limited to O(self.subsample) which is the purpose of this parameter.

AI usage disclosure

I used AI assistance for:

  • Writing this PR description
  • Helping with tests & changelog

@cakedev0
cakedev0 force-pushed the fix/quantile_tr_sparse_no_materialize branch from 6387ccf to edfaebc Compare August 31, 2026 14:59
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
cakedev0 force-pushed the fix/quantile_tr_sparse_no_materialize branch from edfaebc to 814238a Compare August 31, 2026 15:06
@cakedev0
cakedev0 marked this pull request as ready for review September 7, 2026 08:55
@cakedev0

cakedev0 commented Sep 8, 2026

Copy link
Copy Markdown
Member Author

cc @snath-xoc

@cakedev0 cakedev0 added this to Labs Sep 8, 2026
@cakedev0 cakedev0 moved this to PR waiting for reviews in Labs Sep 8, 2026
@Stag51

Stag51 commented Sep 18, 2026

Copy link
Copy Markdown

@cakedev0

@Stag51

Stag51 commented Sep 22, 2026

Copy link
Copy Markdown

@cakedev0 can you please look into these conflict issues

cakedev0 and others added 2 commits September 28, 2026 16:16
…rse_no_materialize

# Conflicts:
#	sklearn/preprocessing/_data.py

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Why this change: #32761 switched QuantileTransformer to averaged_inverted_cdf, and _sparse_column_percentile is shared with RobustScaler. Rather than keeping a "linear" code path only for RobustScaler, I aligned it too:

  • averaged_inverted_cdf is the method for which sample weights are equivalent to repeated samples (it's what _weighted_percentile(..., average=True) computes), so this is a prerequisite to add sample_weight support to RobustScaler later. "linear" doesn't have this property, e.g. np.percentile([0, 1], 25) is 0.25 but np.percentile([0, 0, 1, 1], 25) is 0.
  • center_ is unchanged: for the median, both methods give the same result.
  • scale_ can change, mostly for small samples (e.g. in test_robust_scaler_zero_variance_features, the IQR of [0.5, -0.1, 1.1] goes from 0.6 to 1.2).

Happy to move this to a separate PR if you prefer to keep this one a pure fix.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Yeah why not /:

@cakedev0

Copy link
Copy Markdown
Member Author

I merged main, which required some none trivial conflicts solving because the quantile method changed from linear to average_inverted_cdf.

@jeremiedbb jeremiedbb 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.

Thanks for the PR. The fix looks good and I'm okay to change the quantile method for consistency. I wonder if we can do simpler for the new helper though.

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

Copy link
Copy Markdown
Member

Looks good but could you please run some benchmark for reference before we merge

@cakedev0

cakedev0 commented Oct 1, 2026 •

Copy link
Copy Markdown
Member Author

Investigated with Claude Code, this comment was drafted by Claude.

Looks good but could you please run some benchmark for reference before we merge

Good call, the benchmark caught a regression: on very sparse columns, the PR was ~5x slower than main.

The cause is the Python for loop over percentile_rank in _weighted_percentile. After the argsort, each iteration runs about ten small array operations (scaling the rank, the == 0 mask + nextafter, a searchsorted per feature followed by xp.stack, two xp.clip, fancy indexing, xp.where, ...). Here they all act on arrays with a single element (one column), so the cost is purely interpreter + array_api_compat dispatch overhead (xp.clip alone was ~70% of the profile), ~40µs per iteration. With QuantileTransformer's n_quantiles=1000, that's ~40ms per column, regardless of how many non-zeros the column has. Densifying + np.nanpercentile on main is cheaper than that unless n_samples is huge.

Based on the benchmarks below, I switched to _weighted_percentile_1d_sorted (already used in HGBT binning), which is vectorized over percentiles.

fit time and peak memory (tracemalloc), main vs this PR:

QuantileTransformer(), sparse (CSC)

case main PR main mem PR mem
1e6 x 50, density=1e-4 0.377s 0.002s 15.7 MiB 0.8 MiB
1e6 x 50, density=1e-2 0.209s 0.009s 15.7 MiB 0.9 MiB
1e6 x 50, density=0.1 (subsampled) 0.061s 0.053s 4.8 MiB 4.8 MiB
1e7 x 10, density=1e-4 0.803s 0.001s 152.8 MiB 0.2 MiB

QuantileTransformer(subsample=None), sparse

case main PR main mem PR mem
1e6 x 20, density=1e-3 0.154s 0.001s 15.5 MiB 0.3 MiB
1e6 x 20, density=0.1 0.207s 0.020s 15.5 MiB 2.5 MiB

RobustScaler(with_centering=False), sparse

case main PR main mem PR mem
1e6 x 50, density=1e-4 0.179s 0.002s 15.3 MiB 0.0 MiB
1e6 x 50, density=1e-2 0.202s 0.006s 15.3 MiB 0.3 MiB
1e6 x 20, density=0.1 0.093s 0.019s 15.3 MiB 2.3 MiB

ignore_implicit_zeros=True and dense RobustScaler (linear → averaged_inverted_cdf) are unchanged within noise.

For reference, with _weighted_percentile the first QuantileTransformer row was 1.875s (5x slower than main).

@jeremiedbb

Copy link
Copy Markdown
Member

I opened #35079 to seepd-up weighted_percentile.

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

4 participants