Interview's MateData Science
Interview's Mate

Data Science

Python → ML → MLOps

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
Data Science topics
Lesson 58/ 160
03. Mathematics & Statistics

Eigenvalues, Eigenvectors & SVD

Decompose matrices for PCA intuition, dimensionality reduction, and latent structure.

AdvancedEigenpairs · SVD3 quiz questions
Practice Generative AI Q&A
In plain wordsDiagramTheoryStep by stepWhen to use itInterview cluesMistakes to avoidQuiz (3)Practice

In plain words

Eigenvectors are the special directions where a matrix only stretches things, and eigenvalues say how much; SVD finds the main directions in any data table.

Think of it like…

Squeezing a rubber sheet: most directions get bent and twisted, but along two special axes the sheet just stretches or shrinks. Those axes are the eigenvectors and the stretch factors are the eigenvalues. For data, the axes are the directions along which the cloud is longest and shortest.

Where you’ve seen it

Two standardised features (height and weight) have covariance matrix C = [[3, 1], [1, 3]] (illustrative). Its eigenvalues are 4 and 2, with eigenvectors (1, 1)/√2 and (1, −1)/√2. The first direction, "overall size", carries 4 of the total variance 6 (66.7%), and the second, "height versus weight difference", carries 2 (33.3%). Projecting the data onto the first component reduces two correlated features to one number while keeping two thirds of the variance. In a ratings matrix with 10,000 users × 2,000 movies, a rank-20 truncated SVD summarises tastes in 20 latent factors and powers recommendations.

Words you’ll hear

Eigenvalue and eigenvector
A scalar λ and a direction v with Av = λv.
Covariance matrix
Symmetric matrix of feature covariances; its eigenvectors are principal directions.
Singular value decomposition
X = UΣVᵀ with orthogonal U and V and non-negative singular values in Σ.
Explained variance ratio
Each component's eigenvalue divided by the sum of eigenvalues.

Visual diagram

Loading interactive diagram

Theory & deep dive

Eigenvalues and eigenvectors

Solve Av = λv by finding λ with det(A − λI) = 0. For A = [[3, 1], [1, 3]]: det = (3 − λ)² − 1 = 0, so λ = 4 or λ = 2. For λ = 4, (A − 4I)v = 0 gives v ∝ (1, 1); for λ = 2, v ∝ (1, −1). Check: A(1, 1) = (4, 4) = 4 (1, 1) ✓. The trace equals the sum of eigenvalues (6) and the determinant their product (8), handy sanity checks. Symmetric real matrices, such as covariance and XᵀX, have real eigenvalues and orthogonal eigenvectors, so np.linalg.eigh is the right and stable function.

For a covariance matrix, each eigenvalue is the variance of the data along its eigenvector, and the eigenvectors are the principal directions. A near-zero eigenvalue indicates redundancy (collinear features), which is why very small eigenvalues of XᵀX warn that linear-regression coefficients will be unstable; the condition number (largest ÷ smallest eigenvalue) summarises it.

Eigen-decomposition in NumPy

vals, vecs = np.linalg.eigh(np.array([[3, 1], [1, 3]])) returns vals = [2, 4] (ascending) and the corresponding unit eigenvectors in the columns of vecs.

Key takeaway: Av = λv; for covariance, eigenvalues are variances along orthogonal directions.

SVD, PCA and low-rank approximation

Any m × n matrix X can be written X = UΣVᵀ. For centred data X (n samples × p features), the columns of V are the principal directions, the squared singular values divided by (n − 1) are the eigenvalues of the covariance matrix, and UΣ gives the scores (the data in principal-component coordinates). np.linalg.svd(Xc, full_matrices=False) is the numerically stable route to PCA (scikit-learn's PCA uses it). The explained variance ratio is σᵢ² / Σσⱼ². Choose k so cumulative explained variance reaches a target (for example 90–95%) or by looking for an elbow in the scree plot, or by cross-validated downstream performance.

The Eckart–Young theorem says that truncating to the k largest singular values gives the best rank-k approximation of X in least-squares terms. That justifies image compression, noise removal, latent semantic analysis of text (truncated SVD of a TF-IDF matrix) and matrix-factorisation recommenders (with missing values handled by specialised algorithms). Practical rules: centre the data (and usually standardise features so units don't dominate), remember components are linear and mix features (interpretation via loadings), fit PCA on training data only, and note that PCA preserves variance, not necessarily predictive signal.

PCA with scikit-learn

pca = PCA(n_components=0.95).fit(StandardScaler().fit_transform(X_train)); pca.explained_variance_ratio_.cumsum() keeps the smallest number of components explaining 95% of the variance.

Key takeaway: SVD gives PCA and the best low-rank approximation; centre and scale first.

How it works, step by step

Step 1 of 4
Prepare

1. Centre and scale

Subtract column means and usually divide by standard deviations so no feature dominates by units.

StandardScaler().fit(X_train)
Key numbers at this step
Fit
on training data only

When to use it & trade-offs

What it is

An eigenvector of a square matrix A is a direction that A only stretches (or flips), not turns: Av = λv, where the scalar λ is the eigenvalue. For a symmetric matrix such as a covariance matrix, the eigenvectors are perpendicular and the eigenvalues say how much stretch (variance) lies along each. The singular value decomposition (SVD) generalises this to any matrix: X = UΣVᵀ, where the columns of V are input directions, U output directions, and the singular values in Σ measure each direction's strength. PCA is eigen-decomposition of the covariance matrix, or equivalently SVD of the centred data.

Why it matters: These tools are behind dimensionality reduction (PCA), noise reduction, latent semantic analysis, matrix-factorisation recommenders, image compression and the diagnosis of collinearity. Knowing what an eigenvalue means (variance along a direction) and how SVD gives the best low-rank approximation lets you choose the number of components, interpret loadings and understand why scaling matters before PCA.

When to reach for it

  • Reducing correlated features before modelling
  • Visualising high-dimensional data in 2D
  • Compression and denoising; recommender latent factors

When to avoid or be careful

  • PCA on unscaled features with different units
  • Fitting PCA on all data before the train/test split
  • Assuming high-variance components are the predictive ones

Dimensionality reduction and factorisation

PCA (SVD of centred data)
Best for: Linear compression, decorrelation, visualisation

Strengths: Fast, deterministic, optimal for variance preservation.

Watch out: Linear only; components mix features.

Truncated SVD
Best for: Sparse matrices (text, ratings) without centring

Strengths: Works on sparse data; latent semantic analysis.

Watch out: Not centred, so not exactly PCA.

Non-linear methods (t-SNE, UMAP, autoencoders)
Best for: Visualising and compressing complex structure

Strengths: Capture non-linear manifolds.

Watch out: Harder to interpret; parameters and stochasticity.

Interview signals & numbers to know

When this topic comes up

  • “How does PCA work?”
  • “Eigenvalues of the covariance matrix”
  • “How many components should we keep?”
  • Latent semantic analysis
  • Multicollinearity diagnostics

Benchmarks & rules of thumb

λ = 4, 2 -
eigenvalues of [[3, 1], [1, 3]]
6 and 8 -
trace and determinant of that matrix
σ² / (n − 1) -
covariance eigenvalue from a singular value

Common mistakes & follow-ups

Common interview mistakes

  • Running PCA on raw income (₹ lakhs) and age (years) and letting income dominate the first component.
  • Fitting PCA on the full dataset, leaking information from the test set.
  • Interpreting the sign of a principal component as meaningful.

Likely interviewer follow-ups

  • What do eigenvalues of a covariance matrix represent?
  • How are PCA and SVD related?
  • How do you decide how many principal components to keep?
Interview tip: Work the 2×2 example (λ = 4 and 2, directions (1,1) and (1,−1)). A small exact example shows genuine understanding.

Knowledge check

Loading quiz

Put it into practice

Practice related interview Q&A

Apply Eigenvalues, Eigenvectors & SVD with data science interview Q&A and hands-on practice.

Open Data Science Q&A →Machine Learning Q&A
Previous topic
Vectors, Matrices & Matrix Multiplication
Next topic
Derivatives, Partial Derivatives & Chain Rule