Monday , November 30 2026
Eigenvalues and eigenvectors explained with NumPy: definition, verification, PCA from the covariance matrix, eig vs eigh and other uses.

Eigenvalues and Eigenvectors for Machine Learning (with NumPy)

Updated: 29 November 2026Level: IntermediateReading time: about 9 minutesNumPy · tested

An eigenvector of a matrix is a direction that the matrix only stretches or shrinks, without rotating. The stretch factor is its eigenvalue. They are the maths behind PCA, Google’s PageRank and stability analysis.

What You Will Learn

  • The definition A·v = λ·v
  • Compute eigenvalues and eigenvectors with NumPy
  • Check the result
  • Use them to find principal components
  • eig vs eigh

Prerequisites

Linear Algebra for ML.

Definition

For a square matrix A, a non-zero vector v and a number λ satisfy A·v = λ·v. Multiplying by A gives the same vector, just scaled by λ.

Compute and Check

Example
import numpy as np
np.set_printoptions(precision=3, suppress=True)

A = np.array([[2, 1],
              [1, 2]], dtype=float)
vals, vecs = np.linalg.eigh(A)          # eigh: for symmetric matrices
print(vals)
print(vecs)
v = vecs[:, 1]                           # eigenvector of the largest eigenvalue
print(A @ v, vals[1] * v)
Output
[1. 3.]
[[-0.707  0.707]
 [ 0.707  0.707]]
[2.121 2.121] [2.121 2.121]

Explanation: The eigenvalues are 1 and 3. Eigenvectors are the columns of vecs, scaled to length 1. The direction (1, 1)/√2 is stretched by 3, and (−1, 1)/√2 is left unchanged (λ = 1). The check A·v = λ·v gives identical vectors. Signs of eigenvectors can differ between libraries – both v and −v are valid.

Why They Matter: PCA

Example
rng = np.random.default_rng(0)
x = rng.normal(0, 3, 500)
y = 0.8 * x + rng.normal(0, 1, 500)               # two correlated features
X = np.column_stack([x, y])
X = X - X.mean(axis=0)
C = np.cov(X, rowvar=False)                       # 2x2 covariance matrix
w, V = np.linalg.eigh(C)
w, V = w[::-1], V[:, ::-1]                        # largest first
print(w.round(2), (w / w.sum()).round(3))
Output
[15.58  0.52] [0.967 0.033]

Explanation: The eigenvectors of the covariance matrix are the principal components, and each eigenvalue is the variance along that direction. The first component explains 96.7% of the variance, so these two features could be compressed into one with little information loss – that is dimensionality reduction.

Other Uses

  • PageRank: the ranking vector is the main eigenvector of the web link matrix.
  • Spectral clustering and graph analysis.
  • Stability of dynamic systems and recurrent networks (largest |λ| > 1 can explode).
  • SVD, a related decomposition, powers recommendation systems and image compression.
Function Use
np.linalg.eig Any square matrix (may return complex numbers)
np.linalg.eigh Symmetric matrices – real, sorted, more stable
np.linalg.svd Any matrix; singular values and vectors

Series

Maths & Statistics series: Introduction to Statistics · Descriptive Statistics · Probability · Bayes’ Theorem · Distributions · Inferential Statistics · Hypothesis Testing · P-Values · Correlation & Covariance · Linear Algebra · Eigenvalues · Calculus · Optimization · Interview Questions

Interview Questions

Q: What is an eigenvector?
A non-zero vector whose direction is unchanged by the matrix; it is only scaled by the eigenvalue.

Q: How are eigenvalues used in PCA?
Eigenvectors of the covariance matrix are the components; eigenvalues give the variance each explains.

Practice Exercises

  1. Find the eigenvalues of [[4, 2], [1, 3]] and verify A·v = λ·v.
  2. Run the PCA example with y = 0.2x + noise and compare the explained variance.

Summary

Eigenvectors are directions a matrix only scales; eigenvalues are the scale factors. In ML they reveal the main directions of variance in data (PCA).

Next: Optimization Techniques

About Machine Learning

Check Also

P-Values Explained Simply (with Python Simulation)

What p-values mean, computed exactly and by simulation, the 0.05 threshold, statistical vs practical significance and common mistakes.