Repository navigation
Conversation
Co-authored-by: Shruti Nath <[email protected]>
…dev0/scikit-learn into fix/quantile_tr_sparse_ignore_zeros
Co-authored-by: Saad Shabbir <[email protected]>
6387ccf to
edfaebc
Compare
Credits Saad Shabbir who independently proposed the same fix in scikit-learn#34451, which this PR supersedes. Co-authored-by: Saad Shabbir <[email protected]>
edfaebc to
814238a
Compare
…rse_no_materialize
…rse_no_materialize
|
cc @snath-xoc |
|
@cakedev0 can you please look into these conflict issues |
…rse_no_materialize # Conflicts: # sklearn/preprocessing/_data.py
Co-Authored-By: Claude Opus 5.5 <[email protected]>
There was a problem hiding this comment.
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_cdfis 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 addsample_weightsupport toRobustScalerlater."linear"doesn't have this property, e.g.np.percentile([0, 1], 25)is0.25butnp.percentile([0, 0, 1, 1], 25)is0.center_is unchanged: for the median, both methods give the same result.scale_can change, mostly for small samples (e.g. intest_robust_scaler_zero_variance_features, the IQR of[0.5, -0.1, 1.1]goes from0.6to1.2).
Happy to move this to a separate PR if you prefer to keep this one a pure fix.
|
I merged main, which required some none trivial conflicts solving because the quantile method changed from linear to average_inverted_cdf. |
Co-Authored-By: Claude Opus 5.5 <[email protected]>
jeremiedbb
left a comment
There was a problem hiding this comment.
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.
|
Looks good but could you please run some benchmark for reference before we merge |
Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Investigated with Claude Code, this comment was drafted by Claude.
Good call, the benchmark caught a regression: on very sparse columns, the PR was ~5x slower than main. The cause is the Python Based on the benchmarks below, I switched to
For reference, with |
|
I opened #35079 to seepd-up |
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(andRobustScaler) compute quantiles from sparse columns:QuantileTransformer()may materialize sparse matrices #34298: withignore_implicit_zeros=False, computing the quantiles densified the column into ann_samples-sized array, which can be very slow or OOM for extremely sparse matrices.QuantileTransformer()sub-sampling impact is too high for sparse matrices #34857: when a column hit the subsampling path, onlysubsample * len(column_nnz_data) // n_samplesnon-zero values were kept, this can round down to a handful of points, so large chunks of the output distribution collapse to a single value.To fix #34298, I added
_sparse_column_quantile, which computesnanquantile-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-nanpercentilepattern.For #34857, the subsampling path now draws up to
self.subsamplenon-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 ton_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: