Skip to content

Add sample_weight support to QuantileTransformer #30707

Open

Description

@antipisa

Describe the workflow you want to enable

Would be good to get sample_weight support for QuantileTransformer for dealing with sparse or imbalanced data, a la #15601.

scaler = QuantileTransformer(output_distribution="normal")

scaler.fit(X, sample_weight=w)

Describe your proposed solution

As far as I know it would just require adding the weight argument to the quantiles_ computation in np.nanpercentile.

KBinsDiscretizer supports sample_weight and with strategy='quantile', encode='ordinal' this behavior can be achieved but it is much, much slower.

Describe alternatives you've considered, if relevant

No response

Additional context

No response

Activity

  1. removed
    Needs Triage
    on Jan 23, 2025
  2. lesteve commented on Jan 23, 2025

    @lesteve
    Member

    Searching in the issue tracker, looks like this feature is in scope if I understand correctly #20522 (comment).

    cc @snath-xoc and @jeremiedbb for an informed opinion, since they have been working on sample weights support recently.

  3. changed the title [-]Add sample_weight support to QuantileTransformer?[/-] [+]Add sample_weight support to QuantileTransformer[/+] on Jan 23, 2025
  4. ogrisel commented on Feb 3, 2025

    @ogrisel
    Member

    @antipisa would you be interested in contributing a PR? There is already a common test named check_sample_weight_equivalence_on_dense_data that should be triggered as soon as you add the sample_weight kwarg to fit. Once this is done, the following command should pick it up:

    pytest -k "check_sample_weight_equivalence and QuantileTransformer" sklearn/tests/test_common.py -v  -s 
    

    You might want to adjust some non-default parameter values for that check in PER_ESTIMATOR_CHECK_PARAMS in sklearn/utils/_test_common/instance_generator.py.

    Note that when subsampling is enabled, we need to follow a similar strategy as implemented in KBinsDiscretizer. At the time of writing, the code for KBinsDiscretizer in main is still half broken, but there is a fix that is almost ready to be merged: #29907. You can take a look at this PR for inspiration.

    We are working on tooling to help test the case where the estimator's fit is stochastic (depends on random_state which is the case when subsampling is enabled). You can track the latest progress here: #16298 (comment)

  5. added
    Moderate
    on Feb 3, 2025
  6. kaekkr commented on Apr 3, 2025

    @kaekkr

    I will take this!

  7. Mohataseem89 commented on Dec 8, 2025

    @Mohataseem89

    a quick update: the feature request is now being actively worked on in PR #32761, which builds on the previous effort in #31147 and incorporates the unified _weighted_percentile implementation suggested in the reviews

    the progress looks promising, and it should bring full sample_weight support to QuantileTransformer once finalized.

  8. moved this to In progress in Labson Jun 11, 2026
  9. added this to Labs and removed this from Labson Jun 11, 2026
  10. csadorf commented on Sep 29, 2026

    @csadorf

    QuantileTransformer.fit now accepts sample_weight following the merge of #32761, which appears to address the request here. Is there any remaining work covered by this issue, or can it be closed?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    ModerateNew Feature

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions