PCA Correlation Loadings and Top Contributors

Quick Overview

Implement PCA correlation loadings in NumPy, the Pearson correlation between each feature and each principal component score, and report the strongest positive and negative contributors to the first two components. It tests the eigendecomposition setup, sign conventions, zero-variance handling and stable tie ordering.

PCA Correlation Loadings and Top Contributors

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Implement `correlation_loadings(X, n_components=2)`, returning an `(n_features, n_components)` array whose entry `[j, k]` is the Pearson correlation between feature `j` and the PC `k` scores. Then implement `top_contributors(X, feature_names, n_top=2)`, which reports the strongest positive and negative contributors to PC1 and PC2. It returns a dict keyed by `"PC1"` and `"PC2"`, each holding a `"positive"` and a `"negative"` list of feature names. For example, with columns built from three uncorrelated signals: ```python import numpy as np # columns: a = 3*z1, b = -z1, c = 1.5*z2, d = -z2, noise # z1, z2 and noise are uncorrelated, var(z1) > var(z2) > var(noise) X = np.array([ [ 6, -2, 1.5, -1, 0.5], [-6, 2, 1.5, -1, -0.5], [ 6, -2, -1.5, 1, -0.5], [-6, 2, -1.5, 1, 0.5], ]) correlation_loadings(X, n_components=2) # approximately [[1, 0], [-1, 0], [0, 1], [0, -1], [0, 0]] top_contributors(X, ["a", "b", "c", "d", "noise"], n_top=1) # {"PC1": {"positive": ["a"], "negative": ["b"]}, # "PC2": {"positive": ["c"], "negative": ["d"]}} ``` ```hint Avoiding the projection You can project the data and correlate, or find a closed form. Write the covariance between feature `j` and the score of PC `k` in terms of the covariance matrix and that component's eigenvector. ``` ```hint Tie order `np.argsort` is not stable by default, so equal loadings can come back in any order. Check which sort keeps feature order on ties. ``` ### Constraints & Assumptions - PCA on mean-centered `X`, components ordered by descending variance. - Flip each component so its largest-absolute-weight entry is positive. - Zero-variance features get loading `0`. Clip loadings to `[-1, 1]`. - The `"positive"` list holds names with loading `> 0`, largest first. The `"negative"` list holds names with loading `< 0`, most negative first. Ties keep feature order. - Each list has at most `n_top` names (fewer if not enough qualify). - `X` has at least two rows and two features, and `n_components` is at most the number of features. ### What a Strong Answer Covers - The PCA setup: centring, components in descending variance order, and the sign rule applied to each component. - How the loading is computed (from the scores or from a closed form) and why it equals the Pearson correlation. - Zero-variance features, clipping, and degenerate components with zero variance. - The ranking rules: strict sign filters, the two sort directions, stable ties and truncation to `n_top`. - The cost of building the covariance matrix and decomposing it. ### Follow-up Questions - Why do squared loadings across all components sum to 1 per feature? - How do correlation loadings differ from the raw eigenvector weights? - What happens to the top contributors when PC1 and PC2 have nearly equal eigenvalues?

Overview: Implement PCA correlation loadings in NumPy, the Pearson correlation between each feature and each principal component score, and report the strongest positive and negative contributors to the first two components. It tests the eigendecomposition setup, sign conventions, zero-variance handling and stable tie ordering.

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

Implement correlation_loadings(X, n_components=2), returning an (n_features, n_components) array whose entry [j, k] is the Pearson correlation between feature j and the PC k scores. Then implement top_contributors(X, feature_names, n_top=2), which reports the strongest positive and negative contributors to PC1 and PC2. It returns a dict keyed by "PC1" and "PC2", each holding a "positive" and a "negative" list of feature names.

For example, with columns built from three uncorrelated signals:

import numpy as np

# columns: a = 3*z1, b = -z1, c = 1.5*z2, d = -z2, noise
# z1, z2 and noise are uncorrelated, var(z1) > var(z2) > var(noise)
X = np.array([
    [ 6, -2,  1.5, -1,  0.5],
    [-6,  2,  1.5, -1, -0.5],
    [ 6, -2, -1.5,  1, -0.5],
    [-6,  2, -1.5,  1,  0.5],
])

correlation_loadings(X, n_components=2)
# approximately [[1, 0], [-1, 0], [0, 1], [0, -1], [0, 0]]

top_contributors(X, ["a", "b", "c", "d", "noise"], n_top=1)
# {"PC1": {"positive": ["a"], "negative": ["b"]},
#  "PC2": {"positive": ["c"], "negative": ["d"]}}

Constraints & Assumptions

  • PCA on mean-centered X , components ordered by descending variance.
  • Flip each component so its largest-absolute-weight entry is positive.
  • Zero-variance features get loading 0 . Clip loadings to [-1, 1] .
  • The "positive" list holds names with loading > 0 , largest first. The "negative" list holds names with loading < 0 , most negative first. Ties keep feature order.
  • Each list has at most n_top names (fewer if not enough qualify).
  • X has at least two rows and two features, and n_components is at most the number of features.

What a Strong Answer Covers Guidance

  • The PCA setup: centring, components in descending variance order, and the sign rule applied to each component.
  • How the loading is computed (from the scores or from a closed form) and why it equals the Pearson correlation.
  • Zero-variance features, clipping, and degenerate components with zero variance.
  • The ranking rules: strict sign filters, the two sort directions, stable ties and truncation to n_top .
  • The cost of building the covariance matrix and decomposing it.

Follow-up Questions Guidance

  • Why do squared loadings across all components sum to 1 per feature?
  • How do correlation loadings differ from the raw eigenvector weights?
  • What happens to the top contributors when PC1 and PC2 have nearly equal eigenvalues?