Skip to content

BUG: QuantileTransformer(ignore_implicit_zeros=True) sub-sampling behavior is incorrect #32585

Closed

Description

@cakedev0

Describe the bug

QuantileTransformer(ignore_implicit_zeros=True) sub-sampling behavior is incorrect leading to very incorrect results for very sparse matrices and/or small sub-sample size.
More over, this behavior is "discontinous": see the example.

In those lines:
https://github.com/scikit-learn/scikit-learn/blob/main/sklearn/preprocessing/_data.py#L2830-L2839

column_subsample can easily be 0 if n_samples is big, and I don't see why a big n_samples should change a behavior that triggers only if len(column_nnz_data) > self.subsample...

Steps/Code to Reproduce

import numpy as np
from scipy.sparse import csc_matrix
from sklearn.preprocessing import QuantileTransformer

subsample = 10_000

qt = QuantileTransformer(
    ignore_implicit_zeros=True,
    subsample=subsample,
)

# create a very sparse X matrix with two very similar columns:
n, d = 10**9, 2
# one colunm has size `subsample - 1`, the other one has size `subsample + 1`
col = np.repeat([0, 1], [subsample - 1, subsample + 1])
# choose some row indices (doesn't matter in this example):
row = np.arange(subsample * 2)
# random normal data:
data = np.random.randn(subsample * 2)

X = csc_matrix((
    data,
    (row, col)
), shape=(n, d))

Y = qt.fit_transform(X)
# given that X[:, 0] and X[:, 1] are very similar,
# you would expect they are treated similarly

# Y[:, 0] is as exepcted (looking like a uniform RV):
print(np.quantile(Y[:, 0].data, np.linspace(0, 1, num=11)))

# but Y[:, 1] is all 0 or 1
assert np.all((Y[:, 1].data == 0) | (Y[:, 1].data == 1))
# because all fitted quantiles are zeros, because the section of
# the code pointed in this issue created an array of size 0
assert (qt.quantiles_[:, 1] == 0).all()

Expected Results

Both Y[:, 0].data and Y[:, 1].data should look like uniform RVs (after all, they're almost identical).

Actual Results

See comments in my snippet.

Versions

System:
    python: 3.12.11 (main, Aug 18 2025, 19:19:11) [Clang 20.1.4 ]
executable: /home/arthur/dev-perso/scikit-learn/sklearn-env/bin/python
   machine: Linux-6.14.0-33-generic-x86_64-with-glibc2.39

Python dependencies:
      sklearn: 1.8.dev0
          pip: None
   setuptools: 80.9.0
        numpy: 2.3.4
        scipy: 1.16.2
       Cython: 3.1.5
       pandas: 2.3.3
   matplotlib: 3.10.7
       joblib: 1.5.2
threadpoolctl: 3.6.0

Built with OpenMP: True

threadpoolctl info:
       user_api: blas
   internal_api: openblas
    num_threads: 16
         prefix: libscipy_openblas
       filepath: /home/arthur/dev-perso/scikit-learn/sklearn-env/lib/python3.12/site-packages/numpy.libs/libscipy_openblas64_-8fb3d286.so
        version: 0.3.30
threading_layer: pthreads
   architecture: Haswell

       user_api: blas
   internal_api: openblas
    num_threads: 16
         prefix: libscipy_openblas
       filepath: /home/arthur/dev-perso/scikit-learn/sklearn-env/lib/python3.12/site-packages/scipy.libs/libscipy_openblas-b75cc656.so
        version: 0.3.29.dev
threading_layer: pthreads
   architecture: Haswell

       user_api: openmp
   internal_api: openmp
    num_threads: 16
         prefix: libgomp
       filepath: /usr/lib/x86_64-linux-gnu/libgomp.so.1.0.0
        version: None

Activity

  1. added
    Needs Triage
    on Oct 27, 2025
  2. removed
    Needs Triage
    on Oct 29, 2025
  3. betatim commented on Oct 29, 2025

    @betatim
    Member

    I can reproduce this and also not sure why there aren't always self.subsample samples in column_data (if there are more samples than self.subsample).

  4. cakedev0 commented on Jun 10, 2026

    @cakedev0
    MemberAuthor

    @snath-xoc you might be interested in taking a look at this PR as you're working on #32761

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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions