Foundations to Frontier
Tests
D. Classical Machine Learning › D9 Dimensionality Reduction and Manifold Learning

Incremental PCA

Incremental PCA computes principal components from data that arrive in batches or don't fit in memory. It keeps a compact summary (sample count, mean, the top singular values and their directions) and merges each new batch into it with a small SVD. The merge includes a correction for the shift in the mean. Keeping all components gives exactly the batch result; truncating to k loses a little, especially when the eigenvalues just after k are close to those just before it, and a few extra internal components remove most of that loss. A forgetting factor lets the components track slowly changing data. This page covers the merge update, its exactness, the truncation error, oversampling, forgetting for drift, cost, and related streaming methods (Oja's rule, frequent directions). In the exercises you implement incremental PCA and compare it with batch PCA; then you study batch size, oversampling and forgetting.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

PCA that reads the data a bit at a time

Ordinary PCA needs all the data at once. Incremental PCA keeps a small summary instead: how many points it has seen, their mean, and the few strongest directions with their strengths. When a new batch arrives, it combines the summary with the batch, recomputes the strongest directions, and throws the batch away. Memory stays small however long the data stream is. With a forgetting factor, old data gradually fade, so the directions can follow a slowly changing system.

Incremental PCA on a drifting stream
angle error sin θ 0.900 · effective memory all 60 batches
Dots: the last 5 batches. For the first 30 batches the direction is fixed and every setting finds it. Then it starts rotating. With f = 1 the estimate averages the whole history and lags further and further behind. With f below 1, old batches fade (memory of about 1/(1 − f) batches) and the estimate follows the drift. The price is that each estimate uses fewer points, which matters when the structure is weak or the data noisy (here the cloud is so elongated that even a short memory pins it down).
Why engineers care

Telemetry streams and image archives can be much larger than memory, and their structure drifts as hardware ages or operating conditions change. Incremental PCA builds and updates a reduced model on the fly: on board, at a ground station, or across files on disk. It is in scikit-learn as IncrementalPCA, and the same idea underlies adaptive monitoring and visual trackers.

📝 Quick check (Level 0)
0/3 answered
  1. 1. Incremental PCA processes data…
  2. 2. A forgetting factor below 1 lets the estimate…
  3. 3. Keeping every component in the summary gives…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

The merge by hand

Means. A summary of n = 3 points with mean (0, 0) merges with a batch of m = 1 point at (4, 8). The new mean is (3·0 + 1·4, 3·0 + 1·8)/4 = (1, 2).

Scatter of a union. The total scatter (sum of squared deviations) of two groups equals the sum of their own scatters plus a between-group term n·m/(n + m)·‖mean_A − mean_B‖². For A = (0, 2) and B = (4, 6): 2 + 2 + (2·2/4)·16 = 20, matching the direct computation for (0, 2, 4, 6).

The SVD merge. Stack three blocks:

  • the old summary, rows Sᵢvᵢᵀ for the kept singular values and directions;
  • the new batch, centred on its own mean;
  • one extra row √(nm/(n + m))·(mean_old − mean_new) for the shift between the means.

The SVD of this small matrix gives the new singular values and directions. For n = m = 2 and means 1 and 3, the extra row is √1·(1 − 3) = −2.

Forgetting. Multiply the old summary (its count and singular values) by f before each merge. A batch t merges ago then has weight fᵗ, and the effective memory is 1/(1 − f) batches: 10 for f = 0.9.

The steps in plain English:

  1. Initialise with the first batch: its mean and its SVD (keep the top k, plus a few extra).
  2. For each new batch: update the count and mean, stack the old summary rows, the centred batch and the mean-correction row, and take the SVD.
  3. Keep the top k (plus extra) singular values and directions; discard the batch.
  4. Components are the kept directions; explained variances are the singular values squared divided by (n − 1).
📝 Quick check (Level 1)
0/3 answered
  1. 1. Merging 1 point at 6 into a summary of 2 points with mean 0 gives mean…
  2. 2. Effective memory with f = 0.95?
  3. 3. The extra mean-correction row is needed because…

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 30 minUndergraduate students and practising engineers.

Exactness, truncation and drift

Exact when nothing is discarded. The stacked matrix has exactly the same scatter matrix (Gram matrix of centred data) as all the data seen so far: the old summary reproduces its scatter, and the correction row supplies the between-means term. With all components kept, its SVD gives exactly the batch PCA. Exercise 1, with 20 components kept: identical to rounding error.

Truncation error. Keeping only k components drops the scatter in the other directions at every merge. Those discarded directions can matter later, because a direction that looked weak in one batch can turn out important. The error is largest when the eigenvalues just beyond k are close to those within k (a small eigengap).

  • Exercise 1 (clear gap): the top-5 subspace was within sin θ = 0.004 of full PCA, with eigenvalues within 0.03%.
  • Exercise 2 had a small gap (2.2 versus 2.0). Keeping exactly 5 components gave errors of 0.43 with batches of 20 and 0.05 with batches of 1000.
  • Keeping 5 extra components internally cut the error to about 0.001 at every batch size.

Drift. Without forgetting, the summary averages the whole history. In exercise 2, when the dominant plane rotated halfway through the stream, the final estimate was almost unrelated to the new plane (sin θ 0.91). Forgetting with f = 0.8 tracked it (0.017).

Cost. Each merge is an SVD of a (k + m + 1) × d matrix, O(d(k + m)²), with memory O(kd + md). It is independent of the stream length.

from sklearn.decomposition import IncrementalPCA
ipca = IncrementalPCA(n_components=10, batch_size=500)
for chunk in chunks:              # e.g. numpy memmap slices or files
    ipca.partial_fit(chunk)
Z = ipca.transform(X_new)
🐍 Exercise 1: incremental PCA
Implement the batch-merge update (mean update, stacked summary + centred batch + mean-correction row, truncated SVD) with an optional forgetting factor, and a subspace-angle error; compare with full PCA.
📝 Quick check (Level 2)
0/3 answered
  1. 1. Truncation error is largest when…
  2. 2. A cheap fix for truncation error is…
  3. 3. Without forgetting, after a drift the estimate…

Level 3: Advanced

★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.

Theory

Why the merge is exact. Let Xₐ and X_b be the centred old and new data with means μₐ and μ_b, and let μ be the combined mean. The combined centred scatter is Xₐᵀ Xₐ + X_bᵀ X_b + (nm/(n + m))(μₐ − μ_b)(μₐ − μ_b)ᵀ. The stacked matrix [SVᵀ; X_b − μ_b; √(nm/(n + m))(μₐ − μ_b)ᵀ] has exactly this Gram matrix when SVᵀ represents Xₐ fully. So its right singular vectors and singular values equal those of the full data.

Related streaming methods.

  • Oja’s rule is stochastic gradient ascent on variance, one vector at a time, with a learning rate.
  • Frequent directions (Liberty, 2013) keeps an ℓ × d sketch with a guarantee ‖XᵀX − BᵀB‖ ≤ ‖X‖²_F/ℓ.
  • Randomised sketches form a random projection of the stream.

Incremental SVD trades guarantees for simplicity and accuracy in practice.

Interview questions.

  • How would you compute PCA of 10⁹ rows that don’t fit in memory? Incremental PCA over chunks (or a two-pass covariance accumulation if d is small), keeping a few extra components. For huge d, use randomised or frequent-directions sketches.
  • How do you track a slowly changing subspace? Use a forgetting factor or sliding window, choose the memory 1/(1 − f) to match the drift time-scale, and monitor the subspace angle between successive estimates.
📝 Quick check (Level 3)
0/3 answered
  1. 1. Oja's rule is a form of…
  2. 2. Frequent directions keeps…
  3. 3. If d is small (say 50), an exact streaming alternative is…

Level 4: Expert

★★★★★ Expert⏱ 25 minResearchers: original papers and open problems.

What does truncation cost, and how fast can it track drift?

History.

  • E. Oja (1982) introduced the neural PCA learning rule.
  • M. Brand (2002) developed incremental SVD for streaming data.
  • D. Ross, J. Lim, R.-S. Lin and M.-H. Yang (2008) proposed incremental PCA with mean update and forgetting for visual tracking. scikit-learn’s IncrementalPCA follows this scheme.
  • E. Liberty (2013) introduced frequent directions.

Exercise 2. 4000 points in 30 dimensions, with standard deviations 5, 4, 3, 2.5, 2.2 for the top five and 2.0, 1.8, … after them: a small gap after k = 5. Cells show sin θ against full PCA’s top-5 subspace, keeping 5 or 10 components internally.

batch size keep 5 keep 10
20 0.426 0.0008
50 0.368 0.0008
200 0.207 0.0007
1000 0.052 0.0005

Drift (the dominant plane rotates halfway; error against the new plane at the end of the stream): f = 1.0 → 0.906, f = 0.95 → 0.287, f = 0.8 → 0.017.

  • Oversampling is nearly free and fixes truncation. Five extra internal components made the result essentially exact at every batch size.
  • Without oversampling, small batches hurt. Each merge discards scatter in directions 6–30, and the weak-gap directions it discards keep turning out to matter.
  • Forgetting is essential for drift. Choose the memory 1/(1 − f) (here 5 batches for f = 0.8) to match how fast the system changes.
🐍 Exercise 2 (research): batch size, oversampling and drift
On data with a small eigengap after k = 5, measure incremental PCA's subspace error by batch size, keeping 5 or 10 components internally; then track a dominant plane that rotates mid-stream with forgetting factors 1, 0.95 and 0.8.

Incremental PCA in AI, robotics and aerospace

  • Adaptive health monitoring: PCA models of normal engine behaviour updated as components wear.
  • Visual tracking: an incremental appearance subspace for a tracked target (Ross et al.).
  • Out-of-core analysis of large telemetry or image archives on modest hardware.

References

  1. D. A. Ross, J. Lim, R.-S. Lin, M.-H. Yang (2008). “Incremental learning for robust visual tracking.” International Journal of Computer Vision 77.
  2. M. Brand (2002). “Incremental singular value decomposition of uncertain data with missing values.” Proc. ECCV.
  3. E. Oja (1982). “Simplified neuron model as a principal component analyzer.” Journal of Mathematical Biology 15.
  4. E. Liberty (2013). “Simple and deterministic matrix sketching.” Proc. KDD.
  5. scikit-learn developers. “IncrementalPCA.” Documentation.