PCA via Thin SVD in NumPy

Quick Overview

Implement principal component analysis in NumPy by centring the data and taking a thin SVD, returning scores, components and explained variance ratios. It tests how singular vectors map to principal axes, why a sign convention is needed, and input validation.

PCA via Thin SVD in NumPy

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `pca_svd(X, n_components)` for a data matrix `X` of shape `(n, d)`. Centre the columns, take the thin SVD of the centred data, and return `(scores, components, explained_variance_ratio)` for the top `k = n_components` components. The outputs are: - `components`: shape `(k, d)`, orthonormal rows, ordered by descending variance. Flip each row's sign so its largest-absolute-value entry is positive. - `scores`: shape `(n, k)`, equal to `(X - X.mean(axis=0)) @ components.T`. - `explained_variance_ratio`: shape `(k,)`, each component's variance `s**2 / (n - 1)` divided by the total over all components (all zeros if the total variance is 0). Example: ```python X = [[6, 5], [4, 5], [5, 7], [5, 3]] scores, components, ratio = pca_svd(X, n_components=2) components # [[0, 1], [1, 0]] scores # [[0, 1], [0, -1], [2, 0], [-2, 0]] ratio # [0.8, 0.2] ``` ```hint Reading the variances off the SVD Write the covariance of the centred data in terms of the SVD factors. That tells you which factor holds the axes and how the singular values turn into variances. ``` ### Constraints & Assumptions - If `X` is not 2-D, raise `ValueError`. - If `n_components` is outside `[1, min(n, d)]`, raise `ValueError`. - Results must match `sklearn.decomposition.PCA` (after the sign convention) to `rtol=1e-6`. - Use NumPy for the computation. ### What a Strong Answer Covers - Centring and the thin SVD call, and which factor holds the principal axes. - The sign convention and why the output needs one. - The variance ratio computed against the total over all components, including the zero-variance case. - Validation of the array shape and of `n_components`. - Time and space complexity. ### Follow-up Questions - Why use SVD of the centred data instead of eigendecomposing the covariance matrix? - Why is a sign convention needed at all?

Overview: Implement principal component analysis in NumPy by centring the data and taking a thin SVD, returning scores, components and explained variance ratios. It tests how singular vectors map to principal axes, why a sign convention is needed, and input validation.

General logo
General
Nov 25, 2026
easyMachine Learning EngineerOtherMachine Learning
0
0

Implement pca_svd(X, n_components) for a data matrix X of shape (n, d). Centre the columns, take the thin SVD of the centred data, and return (scores, components, explained_variance_ratio) for the top k = n_components components.

The outputs are:

  • components : shape (k, d) , orthonormal rows, ordered by descending variance. Flip each row's sign so its largest-absolute-value entry is positive.
  • scores : shape (n, k) , equal to (X - X.mean(axis=0)) @ components.T .
  • explained_variance_ratio : shape (k,) , each component's variance s**2 / (n - 1) divided by the total over all components (all zeros if the total variance is 0).

Example:

X = [[6, 5], [4, 5], [5, 7], [5, 3]]
scores, components, ratio = pca_svd(X, n_components=2)
components  # [[0, 1], [1, 0]]
scores      # [[0, 1], [0, -1], [2, 0], [-2, 0]]
ratio       # [0.8, 0.2]

Constraints & Assumptions

  • If X is not 2-D, raise ValueError .
  • If n_components is outside [1, min(n, d)] , raise ValueError .
  • Results must match sklearn.decomposition.PCA (after the sign convention) to rtol=1e-6 .
  • Use NumPy for the computation.

What a Strong Answer Covers Guidance

  • Centring and the thin SVD call, and which factor holds the principal axes.
  • The sign convention and why the output needs one.
  • The variance ratio computed against the total over all components, including the zero-variance case.
  • Validation of the array shape and of n_components .
  • Time and space complexity.

Follow-up Questions Guidance

  • Why use SVD of the centred data instead of eigendecomposing the covariance matrix?
  • Why is a sign convention needed at all?