PCA Compression and Reconstruction Error

Quick Overview

Implement PCA compression in NumPy: keep the fewest principal components that reach a target share of explained variance, reconstruct the data with the mean restored, and report the mean squared error. It tests component selection with an exact-tie rule, input validation, and how dropped variance relates to reconstruction error.

PCA Compression and Reconstruction Error

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Implement `compress_and_reconstruct(X, variance_target)`. Given a data matrix `X` of shape `(n, p)`, center it by its column means and keep the fewest principal components whose cumulative explained-variance ratio is `>= variance_target`. Project onto them, reconstruct (adding the mean back), and return `(X_reconstructed, mse)`, where `X_reconstructed` has the shape of `X` and `mse` is a Python `float`: the mean of `(X - X_reconstructed) ** 2` over all entries. For example: ```python X = [[13, -1], [7, -1], [13, -3], [7, -3]] # explained-variance ratios: [0.9, 0.1] compress_and_reconstruct(X, variance_target=0.85) # -> ([[13, -2], [7, -2], [13, -2], [7, -2]], 0.5) compress_and_reconstruct(X, variance_target=0.9) # ratio exactly reached: k = 1, mse 0.5 compress_and_reconstruct(X, variance_target=0.95) # k = 2: exact reconstruction, mse 0.0 ``` ```hint Choosing k Work with the cumulative sum of the explained-variance ratios. Watch the off-by-one between an index and a component count, and decide how a ratio that lands exactly on the target is treated. ``` ```hint Checking the error The variance you drop determines the reconstruction error. Predict the MSE from the discarded eigenvalues and compare it with what your reconstruction gives. ``` ### Constraints & Assumptions - `X` is 2-D with `n >= 2` rows, otherwise raise `ValueError`. - `variance_target` must lie in `(0, 1]`, otherwise raise `ValueError`. - A cumulative ratio equal to the target (within `1e-12`) counts as reaching it. - If total variance is zero, keep 0 components (the reconstruction is the column mean). - For the number of components `k` your function selects, results must match `sklearn` `PCA(n_components=k).inverse_transform` up to numerical tolerance. ### What a Strong Answer Covers - Centring by the column means and getting each component's explained variance from a decomposition of the centred data. - Selecting the smallest `k` with the tie tolerance, including a target of 1.0 and the zero-variance case. - Reconstructing with the mean added back and averaging the squared error over all `n * p` entries, returned as a Python float. - Input validation (shape, sample count, target range) and the cost of the decomposition. ### Follow-up Questions - How does the MSE relate to the discarded eigenvalues? - Why use SVD of the centered data instead of eigendecomposing the covariance? - sklearn's float `n_components` setting keeps components until the cumulative ratio is strictly above the target. When can it pick a different `k` than your function, and how would you test against it?

Overview: Implement PCA compression in NumPy: keep the fewest principal components that reach a target share of explained variance, reconstruct the data with the mean restored, and report the mean squared error. It tests component selection with an exact-tie rule, input validation, and how dropped variance relates to reconstruction error.

PracHub logo
General
Nov 24, 2026
mediumMachine Learning EngineerOtherMachine Learning
0
0

Implement compress_and_reconstruct(X, variance_target). Given a data matrix X of shape (n, p), center it by its column means and keep the fewest principal components whose cumulative explained-variance ratio is >= variance_target. Project onto them, reconstruct (adding the mean back), and return (X_reconstructed, mse), where X_reconstructed has the shape of X and mse is a Python float: the mean of (X - X_reconstructed) ** 2 over all entries.

For example:

X = [[13, -1], [7, -1], [13, -3], [7, -3]]
# explained-variance ratios: [0.9, 0.1]
compress_and_reconstruct(X, variance_target=0.85)
# -> ([[13, -2], [7, -2], [13, -2], [7, -2]], 0.5)
compress_and_reconstruct(X, variance_target=0.9)   # ratio exactly reached: k = 1, mse 0.5
compress_and_reconstruct(X, variance_target=0.95)  # k = 2: exact reconstruction, mse 0.0

Constraints & Assumptions

  • X is 2-D with n >= 2 rows, otherwise raise ValueError .
  • variance_target must lie in (0, 1] , otherwise raise ValueError .
  • A cumulative ratio equal to the target (within 1e-12 ) counts as reaching it.
  • If total variance is zero, keep 0 components (the reconstruction is the column mean).
  • For the number of components k your function selects, results must match sklearn PCA(n_components=k).inverse_transform up to numerical tolerance.

What a Strong Answer Covers Guidance

  • Centring by the column means and getting each component's explained variance from a decomposition of the centred data.
  • Selecting the smallest k with the tie tolerance, including a target of 1.0 and the zero-variance case.
  • Reconstructing with the mean added back and averaging the squared error over all n * p entries, returned as a Python float.
  • Input validation (shape, sample count, target range) and the cost of the decomposition.

Follow-up Questions Guidance

  • How does the MSE relate to the discarded eigenvalues?
  • Why use SVD of the centered data instead of eigendecomposing the covariance?
  • sklearn's float n_components setting keeps components until the cumulative ratio is strictly above the target. When can it pick a different k than your function, and how would you test against it?