Repository navigation
Labels
Description
Describe the bug and give evidence about its user-facing impact
For very sparse columns falling in the sub-sampling path, the number of actually sampled data will be self.subsample * len(column_nnz_data) // n_samples, which for the default subsample=10_000 and 0.1% sparsity is 10... Which can quickly get noising. This can cause a lot of samples to collapse to 1 after fit-transform, distorting the output distribution weirdly. See the plot below.
Steps/Code to Reproduce
import numpy as np
from scipy.sparse import csc_matrix
from sklearn.preprocessing import QuantileTransformer
import matplotlib.pyplot as plt
subsample = 10_000 # default value
qt = QuantileTransformer(subsample=subsample, random_state=2)
# create a very sparse (0.1% sparsity) X matrix with one column:
n, d = 10**7, 1
# set nnz to `subsample + 1` to use the sub-sampling code path:
nnz = subsample + 1
X = csc_matrix((
np.random.RandomState(4).rand(nnz),
(np.arange(nnz), np.zeros(nnz))
), shape=(n, d))
plt.hist(qt.fit_transform(X).data, bins=100);Expected Results
Actual Results
Versions
System:
python: 3.12.3 (main, Jun 19 2026, 12:46:00) [GCC 13.3.0]
executable: [/home/arthur/scikit-learn/sklearn-env/bin/python](https://file+.vscode-resource.vscode-cdn.net/home/arthur/scikit-learn/sklearn-env/bin/python)
machine: Linux-6.17.0-1032-oem-x86_64-with-glibc2.39
Python dependencies:
sklearn: 1.10.dev0
pip: 26.2.1
setuptools: 78.1.0
numpy: 2.5.1
scipy: 1.18.0
Cython: 3.2.8
pandas: 3.0.5
matplotlib: 3.11.1
joblib: 1.5.3
threadpoolctl: 3.6.0
narwhals: 2.24.0
Built with OpenMP: True
threadpoolctl info:
user_api: blas
internal_api: openblas
num_threads: 14
prefix: libscipy_openblas
filepath: [/home/arthur/scikit-learn/sklearn-env/lib/python3.12/site-packages/numpy.libs/libscipy_openblas64_-017048f4.so](https://file+.vscode-resource.vscode-cdn.net/home/arthur/scikit-learn/sklearn-env/lib/python3.12/site-packages/numpy.libs/libscipy_openblas64_-017048f4.so)
version: 0.3.33.112.0
threading_layer: pthreads
architecture: Haswell
user_api: blas
internal_api: openblas
num_threads: 14
prefix: libscipy_openblas
filepath: [/home/arthur/scikit-learn/sklearn-env/lib/python3.12/site-packages/scipy.libs/libscipy_openblas-5f890258.so](https://file+.vscode-resource.vscode-cdn.net/home/arthur/scikit-learn/sklearn-env/lib/python3.12/site-packages/scipy.libs/libscipy_openblas-5f890258.so)
version: 0.3.31.dev
threading_layer: pthreads
architecture: Haswell
user_api: openmp
internal_api: openmp
num_threads: 14
prefix: libgomp
filepath: [/usr/lib/x86_64-linux-gnu/libgomp.so.1.0.0](https://file+.vscode-resource.vscode-cdn.net/usr/lib/x86_64-linux-gnu/libgomp.so.1.0.0)
version: NoneInterest in fixing the bug
Yes, I'm about to open a PR that fixes it.
Activity