Principal Component Analysis (PCA) — ML Technical Interview Questions & Answers
Projecting data onto its highest-variance directions.
Principal Component Analysis (PCA)
- Simple meaning: PCA finds new axes where the cloud of students spreads the most. First principal component = the direction of biggest variance. Second is the next, at right angles.
- Picture: hours and “minutes studied” point the same way. PCA’s first axis is that diagonal. You can plot PC1 vs PC2 and still see groups.
- sklearn: PCA(n_components=2).fit_transform. Scale first (StandardScaler). explained_variance_ratio_ tells how much spread you kept.
- Trap: treating PCs as ‘the meaning of hours’ without checking loadings. Trap: PCA on the full sheet including test, then splitting.
Technical Interview Tip: Rotate to the spread. Scale. 2 PCs for a plot.
Code Examples for Principal Component Analysis (PCA)
Principal Component Analysis (PCA) — working example
# PCA-style intuition: project 2D → 1D by a direction vector
points = [(1.0, 1.0), (2.0, 2.0), (3.0, 2.8)]
# unit direction along y = x
ux, uy = 0.7071, 0.7071
proj = [x * ux + y * uy for x, y in points]
print([round(v, 3) for v in proj])
# Tiny ensemble: average two model votes
votes = [1, 0, 1]
v2 = [1, 1, 1]
ens = [1 if a + b >= 2 else 0 for a, b in zip(v1, v2)]
print(ens)Principal Component Analysis (PCA) — dim reduction compresses features; ensembles combine models; deep nets/RL/NLP are specialized families — practice the core math idea first.
How to Explain Principal Component Analysis (PCA) to an Interviewer
What does PC1 capture?
The direction of most variance
Must you scale?
Yes, usually Different units break PCA