Sub topic 4
Eigenvectors and Eigenvalues in Brief
This chapter assumes you know matrix multiplication, the idea of a basis, and what a covariance matrix is from the earlier chapters. You do not need a full linear algebra course. You need the one fact that makes PCA work: a symmetric matrix has a set of perpendicular directions along which it only stretches.
The defining equation
A matrix A transforms vectors. Most vectors change direction when you multiply them by A. An eigenvector is a nonzero vector v whose direction does not change. It is only scaled:
A v = λ vThe scalar λ (lambda) is the eigenvalue for v. If λ = 3, then A stretches v to three times its length. If λ is negative, v flips direction. If λ = 0, A collapses v to zero.
An eigenvector's length does not matter, because any multiple of v also satisfies the equation. By convention, libraries return eigenvectors with length 1.
Why symmetric matrices are special
A covariance matrix C is symmetric: C[i, j] == C[j, i]. The spectral theorem says that a real symmetric matrix has these properties:
- All eigenvalues are real numbers.
- You can choose
neigenvectors that are mutually perpendicular (orthogonal) and have length 1. Ccan be written asC = V Λ Vᵀ. Here the columns ofVare the eigenvectors andΛis a diagonal matrix of the eigenvalues.
A covariance matrix is also positive semi-definite, so every eigenvalue is zero or larger. The eigenvectors form a new basis for the feature space. In that basis, the covariance matrix is diagonal, which means the new coordinates are uncorrelated. The next chapter shows that this basis is exactly what PCA computes, and that each eigenvalue is the variance along its eigenvector.
Computing it with NumPy
Use np.linalg.eigh for symmetric matrices. It is faster and more accurate than the general np.linalg.eig, and it guarantees real output.
import numpy as np
C = np.array([[4.0, 2.0],
[2.0, 3.0]])
eigvals, eigvecs = np.linalg.eigh(C)
print(eigvals)
v = eigvecs[:, 1]
print(C @ v)
print(eigvals[1] * v)
print(np.allclose(eigvecs.T @ eigvecs, np.eye(2)))
print(np.allclose(eigvecs @ np.diag(eigvals) @ eigvecs.T, C))The first check confirms C v = λ v. The last two confirm that the eigenvectors are orthonormal and that V Λ Vᵀ rebuilds C.
Common errors
- Eigenvectors are columns, not rows.
eigvecs[:, i]pairs witheigvals[i]. Readingeigvecs[i]gives a row, which is a mistake. eighsorts in ascending order. PCA needs the largest eigenvalue first, so reverse the order withnp.argsort(eigvals)[::-1]and reorder the columns the same way.- The sign of an eigenvector is arbitrary.
vand-vare both valid. Two libraries can return opposite signs for the same component. Compare directions, not signs. - Using
eigon a covariance matrix. It can return complex numbers from rounding error. Useeigh. - Repeated eigenvalues. If two eigenvalues are equal, any orthonormal pair in their plane is valid. The individual directions are then not unique.
Exercise
A 3×3 covariance matrix has eigenvalues 5.0, 1.0 and 0.0. Which eigenvector points along the direction of greatest variance? What does the zero eigenvalue tell you about the data?
Show answer
The eigenvector for eigenvalue 5.0 points along the direction of greatest variance, because each eigenvalue is the variance along its eigenvector. The zero eigenvalue means the data has no spread along its eigenvector. The data lies in a 2D plane inside the 3D space, so one feature is a linear combination of the others.
Exercise
You call eigvals, eigvecs = np.linalg.eigh(C) and want the eigenvector with the largest eigenvalue. Which index do you use, and why?
Show answer
Use eigvecs[:, -1]. eigh returns eigenvalues in ascending order, so the largest is last, and eigenvectors are stored as columns.