PCA Biplot Scores and Loading Arrows

Quick Overview

Implement a NumPy function that returns the PC1 and PC2 scores of mean-centred data and the feature loading arrows for a PCA biplot, scaled by one common factor. It tests how principal components are computed, how to make their signs deterministic, and how to match scikit-learn output.

PCA Biplot Scores and Loading Arrows

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Implement `pca_biplot_data(X)` for a 2-D array `X` of shape `(n_samples, n_features)`. Return a tuple `(scores, arrows)`: the PC1/PC2 projections of the mean-centred data, and each feature's unit loading on PC1/PC2 multiplied by one common scale factor. For example: ```python X = [[4, 5], [2, 5], [3, 5.5], [3, 4.5]] scores, arrows = pca_biplot_data(X) # scores -> [[1, 0], [-1, 0], [0, 0.5], [0, -0.5]] # arrows -> [[1, 0], [0, 1]] # longest arrow length 1 == max |score| ``` ```hint Finding the components Centre each column first. The thin SVD of the centred matrix gives the principal axes, already ordered by the variance they explain. ``` ```hint Deterministic signs A principal axis is only defined up to its sign. Fix the sign on the component itself before you compute anything from it, so the scores and the arrows flip together. ``` ### Constraints & Assumptions - `scores` has shape `(n_samples, 2)` and `arrows` has shape `(n_features, 2)`, both NumPy arrays. - Flip each component so its largest-absolute entry is positive (a zero entry counts as positive). - The scale factor is the max absolute entry of `scores` divided by the max Euclidean row length of the loadings (use `0` if that length is `0`). - PC1 has the largest variance. Results must match `sklearn` PCA (after sign fixing) to `rtol=1e-7`. - `n_samples >= 3`, `n_features >= 2`, all values finite. ### What a Strong Answer Covers - Centring by the column means and taking PC1 and PC2 from the decomposition in order of explained variance. - A deterministic sign rule applied to the components before the scores are computed, so the output matches sklearn after the same fix. - The single scale factor (largest absolute score over the longest loading row) and why one shared factor keeps the arrows comparable. - Output shapes, the cost of the decomposition, and degenerate inputs such as constant or rank-deficient data. ### Follow-up Questions - Why can't you read feature correlations directly from these scaled arrows? - How would standardizing features change the biplot?

Overview: Implement a NumPy function that returns the PC1 and PC2 scores of mean-centred data and the feature loading arrows for a PCA biplot, scaled by one common factor. It tests how principal components are computed, how to make their signs deterministic, and how to match scikit-learn output.

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

Implement pca_biplot_data(X) for a 2-D array X of shape (n_samples, n_features). Return a tuple (scores, arrows): the PC1/PC2 projections of the mean-centred data, and each feature's unit loading on PC1/PC2 multiplied by one common scale factor.

For example:

X = [[4, 5], [2, 5], [3, 5.5], [3, 4.5]]
scores, arrows = pca_biplot_data(X)
# scores -> [[1, 0], [-1, 0], [0, 0.5], [0, -0.5]]
# arrows -> [[1, 0], [0, 1]]   # longest arrow length 1 == max |score|

Constraints & Assumptions

  • scores has shape (n_samples, 2) and arrows has shape (n_features, 2) , both NumPy arrays.
  • Flip each component so its largest-absolute entry is positive (a zero entry counts as positive).
  • The scale factor is the max absolute entry of scores divided by the max Euclidean row length of the loadings (use 0 if that length is 0 ).
  • PC1 has the largest variance. Results must match sklearn PCA (after sign fixing) to rtol=1e-7 .
  • n_samples >= 3 , n_features >= 2 , all values finite.

What a Strong Answer Covers Guidance

  • Centring by the column means and taking PC1 and PC2 from the decomposition in order of explained variance.
  • A deterministic sign rule applied to the components before the scores are computed, so the output matches sklearn after the same fix.
  • The single scale factor (largest absolute score over the longest loading row) and why one shared factor keeps the arrows comparable.
  • Output shapes, the cost of the decomposition, and degenerate inputs such as constant or rank-deficient data.

Follow-up Questions Guidance

  • Why can't you read feature correlations directly from these scaled arrows?
  • How would standardizing features change the biplot?