Streaming Incremental PCA with partial_fit
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: hard
Interview Round: Other
Implement `StreamingPCA(n_components)`. Its `partial_fit(X)` consumes one mini-batch `X` of shape `(b, d)` and updates the model without storing past rows. Results must match scikit-learn's `IncrementalPCA` fed the same batches. Also implement `transform` and `inverse_transform`.
The class must meet these requirements:
- `partial_fit` returns `self` and sets `n_samples_seen_`, the exact running `mean_`, `components_` (shape `(k, d)`, orthonormal rows) and `singular_values_`, sorted in descending order.
- `explained_variance_ = S^2 / (n - 1)`, where `n` is the number of rows seen. `explained_variance_ratio_ = S^2 / total_ss`, where `total_ss` is the sum of squares of all rows seen about the running mean. Use zeros if that total is 0.
- Sign convention: the largest-absolute loading of each component is positive.
- `transform(X) = (X - mean_) @ components_.T` and `inverse_transform(Z) = Z @ components_ + mean_`.
- Raise `ValueError` if `X` is not 2-D, `d` changes between calls, `k` is outside `[1, d]`, a batch has fewer than `k` rows, or the first batch has fewer than 2 rows.
For example:
```python
m = StreamingPCA(2)
for batch in (X[:10], X[10:22], X[22:47]): # X shape (47, 3)
m.partial_fit(batch)
m.n_samples_seen_ # 47
m.mean_ # == X.mean(axis=0)
m.components_.shape # (2, 3)
```
```hint Compress the past
The current singular values and components describe the scatter of everything seen so far. Look for a small matrix whose Gram matrix reproduces that scatter, so it can be stacked with the new rows.
```
```hint Merging two groups
Scatter about a merged mean is more than the sum of the two within-group scatters. Work out what the gap between the old mean and the batch mean contributes.
```
### Constraints & Assumptions
- Only `numpy` is required.
- The stored state may not grow with the number of rows seen.
- With `k = d`, results must equal an eigendecomposition of the full-data covariance.
- Matching `IncrementalPCA` means agreement up to floating-point tolerance, after the sign convention above.
### What a Strong Answer Covers
- The stacked-matrix update and why its Gram matrix equals the scatter of all rows seen about the merged mean.
- An exact running mean and running total sum of squares, including the between-group term.
- The sign convention, truncation to `k`, and the variance and ratio definitions, including the zero-total case.
- Input validation for every listed error case.
- The per-call time and memory cost, and where the truncated update stops being exact.
### Follow-up Questions
- What is the memory and time cost per `partial_fit` call?
- How would you adapt the update to drifting data streams?
- Why is the mean-correction row needed in the stacked matrix?
Overview: Implement a streaming PCA class whose partial_fit updates the mean, components and singular values from mini-batches without storing past rows, matching scikit-learn's IncrementalPCA. Tests the stacked SVD update, merging means and scatter across batches, sign conventions and per-call cost.