Repository navigation
Description
Warning
This issue is not yet ready for a PR. If you are interested in contributing to scikit-learn, please have a look at our contributing guidelines, and in particular the sections for new contributors and the "Needs triage" label.
Introduce yourself
No response
Describe the bug and give evidence about its user-facing impact
IncrementalPCA.noise_variance_ is underestimated whenever the data is fitted in more than one batch. The error grows with the number of batches. For standard Gaussian data, the estimate is roughly the true noise variance divided by the number of batches (~0.01 instead of ~0.99 with 100 batches).
Because get_covariance() and get_precision() depend on noise_variance_, both are also affected. For example, the estimated covariance severely underestimates the average feature variance (see the trace below), and the precision matrix is inflated by orders of magnitude.
Steps/Code to Reproduce
import numpy as np
from sklearn.decomposition import PCA, IncrementalPCA
rng = np.random.RandomState(0)
X = rng.standard_normal((10_000, 50))
pca = PCA(n_components=5).fit(X)
print(f"PCA: noise_variance_={pca.noise_variance_:.4f}, "
f"mean diag of get_covariance()={np.diag(pca.get_covariance()).mean():.4f}")
for batch_size in [10_000, 1_000, 100]:
ipca = IncrementalPCA(n_components=5, batch_size=batch_size).fit(X)
print(f"IPCA batch={batch_size:>6}: noise_variance_={ipca.noise_variance_:.4f}, "
f"mean diag of get_covariance()={np.diag(ipca.get_covariance()).mean():.4f}")Expected Results
noise_variance_ should be close to 1 (and close to PCA's value of 0.9851) regardless of batch_size, and the mean diagonal of get_covariance() should be close to 1 (≈ the data's average per-feature variance).
Actual Results
PCA: noise_variance_=0.9851, mean diag of get_covariance()=0.9988
IPCA batch= 10000: noise_variance_=0.9851, mean diag of get_covariance()=0.9988
IPCA batch= 1000: noise_variance_=0.1005, mean diag of get_covariance()=0.1952
IPCA batch= 100: noise_variance_=0.0103, mean diag of get_covariance()=0.1117
Versions
System:
python: 3.13.18 (main, Oct 26 2026, 15:28:15) [GCC 13.3.0]
executable: /usr/bin/python3
machine: Linux-6.6.159+-x86_64-with-glibc2.39
Python dependencies:
sklearn: 1.6.1
pip: 26.2.2
setuptools: 80.10.2
numpy: 2.4.6
scipy: 1.16.3
Cython: 3.0.12
pandas: 2.2.3
matplotlib: 3.10.0
joblib: 1.6.0
threadpoolctl: 3.7.0
Built with OpenMP: True
threadpoolctl info:
user_api: blas
internal_api: openblas
num_threads: 2
prefix: libscipy_openblas
filepath: /usr/local/lib/python3.13/dist-packages/numpy.libs/libscipy_openblas64_-32a4b2a6.so
version: 0.3.31.188.0
threading_layer: pthreads
architecture: Haswell
user_api: blas
internal_api: openblas
num_threads: 2
prefix: libscipy_openblas
filepath: /usr/local/lib/python3.13/dist-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: 2
prefix: libgomp
filepath: /usr/local/lib/python3.13/dist-packages/scikit_learn.libs/libgomp-a34b3233.so.1.0.0
version: NoneInterest in fixing the bug
I can make a PR
The issue seems to be in partial_fit, where noise_variance_ is computed from the remaining explained variance after each SVD. This calculation doesn't account for variance discarded in previous batches.
Since var_ already tracks the total feature variance, a possible fix is to subtract the retained explained variance from the total variance, similar to #9108.
Activity
ege-arhan commentedon Nov 28, 2026
Reproduced on main (
8fb0da7). Your numbers match here to the last digit: PCA0.9851, IPCA0.9851/0.1005/0.0103forbatch_size10000 / 1000 / 100, and the mean diagonal ofget_covariance()drops to0.1117.The cause is in
partial_fit, right at the end:explained_variancecomes from the SVD of the stack of (retained components of the previous data, whitened current batch, mean correction). That stack only carries the retained directions from earlier batches; the energy in the directions that were dropped never re-enters it. So the sum of the stacked spectrum is the current batch's variance plus the retained part, not the total variance of the data seen so far. After a few batches it is far smaller than the real trace, and averaging its tail gives something close to zero.With
n_components=5,batch_size=100, 1000 samples of 20 features:sum(singular_values_**2) / (n-1) = 5.5whilenp.trace(np.cov(X.T)) = 19.6. The 45 tail eigenvalues being averaged describe the tail of the last stack, not the 15 discarded directions of the full covariance.A fix that keeps PCA's definition: the total variance is already tracked exactly in
var_/n_samples_seen_, sowhich gives
0.9851/0.9933/0.9959for the three batch sizes above, and0.0whenn_components == n_features, as before. The0.9933vs0.9851gap atbatch_size=1000is the retained-part estimate differing from PCA's, not the residual.I have the patch and a parametrized test (
batch_size10000/1000/100 againstPCA, plusget_covariance()) locally: the test fails on main and passes with the change, andsklearn/decomposition/testsruns 1112 passed, 234 skipped.I can open a PR if a maintainer agrees this is the right way to compute it. An AI assistant helped with the investigation and drafting; I ran every number above myself.
ajaycodesitbetter commentedon Nov 28, 2026
I can reproduce this on 1.9.1 with the numbers above. Following the analysis by @ege-arhan, I tried computing noise_variance_ from the running total variance: (sum(var_) * n/(n-1) - sum(explained_variance_)) / (min(n_samples_seen, n_features) - n_components), clipped at 0. With the issue's example it gives about 0.994 at batch_size=1000 and 0.995 at 100 instead of 0.10 and 0.01, matches PCA when the last batch has only n_components rows, and the existing IncrementalPCA tests pass. I have a regression test ready. Happy to open a PR once a maintainer confirms the approach and removes Needs Triage.
waseem1302-x commentedon Nov 30, 2026
Hi, I reproduced this on current main (fc603cd). I ran the issue example plus a probe over batch sizes, k values, float32, and manual partial_fit calls. On the example (10,000 by 50, k=5), PCA gives noise_variance_ 0.9851, while IncrementalPCA gives 0.1005 for batches of 1,000 and 0.0103 for batches of 100. The bug is real and gets worse with more batches.
The direction in the thread checks out. Computing the noise from the discarded variance (total minus retained) gives 0.9933 and 0.9959, matching the numbers above, and get_covariance/get_precision come back to about 1.00. Two details: the denominator has to be min(n_features, n_samples) - k, not n_features - k (on 40 samples, 100 features, k=5, the n_features form gives 0.7505 against PCA's 1.9835), and the estimate has to be capped at the smallest retained variance, or get_precision divides by zero when a retained variance drops to the noise level.
One limit worth noting: when the retained directions change a lot between batches, the estimate can be off in either direction (up to 100x in a constructed case), since it is total minus retained, not the true residual.
I am not opening a PR: the issue still has Needs Triage and you both have a patch ready. Happy to share my local verification and tests if a maintainer wants them.