PCA With and Without Standardisation

Quick Overview

Implement z-score standardisation and covariance-based PCA in NumPy, then compare explained variance ratios and the first principal component with and without scaling. It tests eigendecomposition details, sign conventions and how feature scale affects which feature drives PC1.

PCA With and Without Standardisation

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement three functions on a feature matrix `X` of shape `(n, d)`. `standardize` z-scores each column, `pca_summary` runs PCA on centred or standardised data, and `compare_scaling` reports how a large-scale feature dominates PC1 without scaling. The functions must meet these requirements: - `standardize(X)`: subtract the column mean and divide by the population std (`ddof=0`). Constant columns divide by 1, so they become all zeros. - `pca_summary(X, scale=False)` returns `(ratio, components)`: eigendecompose the covariance (divisor `n - 1`) of the centred data, or of the standardised data when `scale` is true. The result must match sklearn `PCA`. - `ratio` has length `d`, is sorted in descending order and sums to 1. `components` is `(d, d)` with orthonormal rows in the same order. - Flip each component's sign so its largest-absolute loading is positive. - `compare_scaling(X)` returns a dict with keys `raw_ratio`, `scaled_ratio`, `raw_pc1`, `scaled_pc1`, `raw_dominant` and `scaled_dominant`. The two `_dominant` values are the Python `int` index of the largest-absolute PC1 loading. Example: ```python X = [[1, 0], [-1, 0], [0, 10], [0, -10]] pca_summary(X, scale=False) # ratio = [200/202, 2/202], components[0] = [0, 1] pca_summary(X, scale=True) # ratio = [0.5, 0.5]; the two eigenvalues tie, so the components are not unique here ``` ```hint Ordering the eigenpairs Check the order in which `np.linalg.eigh` returns eigenvalues, how it lays out the eigenvectors, and what rounding can do to eigenvalues that should be zero. ``` ### Constraints & Assumptions - Use NumPy only. - Inputs may be lists, so convert with `np.asarray(X, dtype=float)`. - `X` is 2-D with shape `(n, d)`. ### What a Strong Answer Covers - The standardisation formula, including the `ddof=0` choice and constant columns. - Building the covariance with the `n - 1` divisor, ordering the eigenpairs, and applying the sign rule. - Why PC1 follows the largest-variance feature on raw data, and what changes after scaling. - Degenerate inputs such as tied eigenvalues or zero total variance. - The time and space cost of the eigendecomposition approach. ### Follow-up Questions - When would you not standardise before PCA? - Why do the ratios equal 0.5 each in the standardised example? - How would the results change if `standardize` used `ddof=1`?

Overview: Implement z-score standardisation and covariance-based PCA in NumPy, then compare explained variance ratios and the first principal component with and without scaling. It tests eigendecomposition details, sign conventions and how feature scale affects which feature drives PC1.

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

Implement three functions on a feature matrix X of shape (n, d). standardize z-scores each column, pca_summary runs PCA on centred or standardised data, and compare_scaling reports how a large-scale feature dominates PC1 without scaling.

The functions must meet these requirements:

  • standardize(X) : subtract the column mean and divide by the population std ( ddof=0 ). Constant columns divide by 1, so they become all zeros.
  • pca_summary(X, scale=False) returns (ratio, components) : eigendecompose the covariance (divisor n - 1 ) of the centred data, or of the standardised data when scale is true. The result must match sklearn PCA .
  • ratio has length d , is sorted in descending order and sums to 1. components is (d, d) with orthonormal rows in the same order.
  • Flip each component's sign so its largest-absolute loading is positive.
  • compare_scaling(X) returns a dict with keys raw_ratio , scaled_ratio , raw_pc1 , scaled_pc1 , raw_dominant and scaled_dominant . The two _dominant values are the Python int index of the largest-absolute PC1 loading.

Example:

X = [[1, 0], [-1, 0], [0, 10], [0, -10]]
pca_summary(X, scale=False)
# ratio = [200/202, 2/202], components[0] = [0, 1]
pca_summary(X, scale=True)
# ratio = [0.5, 0.5]; the two eigenvalues tie, so the components are not unique here

Constraints & Assumptions

  • Use NumPy only.
  • Inputs may be lists, so convert with np.asarray(X, dtype=float) .
  • X is 2-D with shape (n, d) .

What a Strong Answer Covers Guidance

  • The standardisation formula, including the ddof=0 choice and constant columns.
  • Building the covariance with the n - 1 divisor, ordering the eigenpairs, and applying the sign rule.
  • Why PC1 follows the largest-variance feature on raw data, and what changes after scaling.
  • Degenerate inputs such as tied eigenvalues or zero total variance.
  • The time and space cost of the eigendecomposition approach.

Follow-up Questions Guidance

  • When would you not standardise before PCA?
  • Why do the ratios equal 0.5 each in the standardised example?
  • How would the results change if standardize used ddof=1 ?