推导 PCA;主成分为什么是 eigenvector?
保留几维怎么选?

英文问法Derive PCA. · Why are the principal components eigenvectors of the covariance matrix? · How does PCA relate to SVD? · How many components do you keep?

考过微软 Teams AS IC2(Principal AS 让推导 eigenvalue equation)· Glassdoor AS II

先弄清楚:每个概念是什么

PCA
给数据换一组坐标轴,按数据在各方向上散得多开排序,只留前几个方向。例:身高和体重高度相关,可以合成一个方向,2 维压成 1 维。
Variance 与主成分
所有点投影到一个方向上散得多开,就是这个方向的 variance。第一主成分是 variance 最大的方向,第二主成分是和它垂直的方向里最大的。
Covariance matrix \(\Sigma\)
记录每两维一起变的程度。方向 \(w\) 上的 variance 是 \(w^\top\Sigma w\)。
Eigenvector 与 eigenvalue
被 \(\Sigma\) 作用后只伸缩、不转向的方向是 eigenvector,伸缩倍数是 eigenvalue,在 PCA 里正好是这个方向上的 variance。
Explained variance ratio
某个方向的 variance 占总 variance 的比例,\(\lambda_j / \sum \lambda\)。

30 秒回答

PCA finds orthogonal directions of maximum variance. After centering, the variance of the data projected onto a unit vector w is wᵀΣw. Maximizing it subject to ‖w‖ = 1 with a Lagrange multiplier gives Σw = λw, so w is an eigenvector of the covariance matrix and the projected variance equals its eigenvalue; we keep the top-k eigenvectors. Equivalently, this minimizes reconstruction error. In practice we compute it with an SVD of the centered data, where the principal directions are the right singular vectors, and choose k by the cumulative explained variance ratio, for example enough to keep 95%.

推导

为什么是 eigenvector(Teams AS 原题)

数据先 centering。投影到单位向量 \(w\) 上的 variance:

\[ \frac1n\sum_i (w^\top x_i)^2 = w^\top \Sigma\, w,\qquad \Sigma = \frac1n X^\top X \]

在 \(w^\top w = 1\) 的约束下最大化,用 Lagrange multiplier:

\[ L = w^\top\Sigma w - \lambda(w^\top w - 1),\qquad \nabla_w L = 2\Sigma w - 2\lambda w = 0 \;\Longrightarrow\; \Sigma w = \lambda w \]

代回去 \(w^\top\Sigma w = \lambda\),投影的 variance 就是 eigenvalue,所以取最大的那个,后面依次取第 2、3… 大的 eigenvector。

等价于最小化 reconstruction error

保留前 \(k\) 个方向时,reconstruction error 等于被丢掉的 eigenvalue 之和 \(\sum_{j>k}\lambda_j\)(Murphy eq. 20.26)。总 variance 固定,所以两个目标等价。

手算例子

4 个点:(2, 2)、(−2, −2)、(1, −1)、(−1, 1),均值已经是 0。

\[ \Sigma = \frac14\sum_i x_i x_i^\top = \begin{pmatrix} 2.5 & 1.5 \\ 1.5 & 2.5 \end{pmatrix} \] \[ \lambda_1 = 4,\ w_1 = \tfrac{1}{\sqrt2}(1, 1);\qquad \lambda_2 = 1,\ w_2 = \tfrac{1}{\sqrt2}(1, -1) \]

代码(NumPy,用 SVD)

def pca(X, k):
    Xc = X - X.mean(0)                     # centering
    U, S, Vt = np.linalg.svd(Xc, full_matrices=False)
    W = Vt[:k].T                           # (d, k) 前 k 个主方向
    var = S**2 / len(X)                    # 各方向 variance = eigenvalue
    # 返回:投影、主方向、explained variance ratio
    return Xc @ W, W, var[:k] / var.sum()

复杂度

做法计算量
先算 \(X^\top X\),再做 eigendecomposition\(O(nd^2) + O(d^3)\)
直接对 \(X\) 做 exact SVD\(O(nd^2) + O(d^3)\)
只要前 \(k\) 个:randomized SVD\(O(nk^2) + O(k^3)\)

保留几维怎么选

reconstruction error 随 \(k\) 一直变小(\(k = d\) 时为 0),不能直接取最小。把 eigenvalue 从大到小排,算 explained variance ratio 的累计值,取第一次超过阈值(比如 95%)的 \(k\)。也可以画 scree plot 找拐点,或按下游任务的指标选。

例:5 维数据的 eigenvalue 是 5、3、1、0.5、0.5,总和 10。

k12345
单个占比50%30%10%5%5%
累计50%80%90%95%100%

阈值 95% → \(k = 4\),阈值 90% → \(k = 3\)。sklearn 里写 PCA(n_components=0.95) 会自动选。

必答点

追问

为什么要先 centering?

\(w^\top\Sigma w\) 是 variance 的前提是均值为 0。不减均值,第一主成分会被拉向均值所在的方向。

要不要 standardize?

各维量纲不同时要,否则数值范围大的那一维会主导第一主成分(Murphy §20.1,p.662)。

PCA 和 SVD 什么关系?为什么实际用 SVD?

centered \(X = USV^\top\),则 \(X^\top X = VS^2V^\top\),所以 \(V\) 的列就是主方向,eigenvalue \(= s^2/n\)。用 SVD 不需要显式算 \(X^\top X\),数值上更稳。

维度 d 比样本数 n 还大怎么办?

改算 \(n\times n\) 的 \(XX^\top\) 的 eigenvector 再换算回来(Bishop §12.1.4)。

PCA 有什么局限?

只能找 linear 方向,非线性结构用 kernel PCA。它是 unsupervised 的,variance 最大的方向不一定对分类最有用,要用 label 就换 LDA。

来源Murphy, PML1 §20.1(pp.657–665)· Bishop, PRML §12.1(pp.561–569)