如何在PCA中进行矩阵归一化
我正在使用Python,并且按照这个教程实现了主成分分析(PCA)。
一切都运行得很好,我得到了协方差,成功进行了转换,并且顺利地把数据还原到了原来的维度。
但是,我该如何进行白化处理呢?我尝试将特征向量除以特征值:
S, V = numpy.linalg.eig(cov)
V = V / S[:, numpy.newaxis]
然后用V来转换数据,但这导致了奇怪的数据值。有人能帮我解释一下吗?
4 个回答
1
我觉得你需要把 V 转置一下,然后对 S 开平方根。所以公式是:
要和数据相乘的矩阵 = 转置(V) * S 的平方根的倒数
15
如果你使用Python的scikit-learn库来做这个,你只需要设置一个内置的参数就可以了。
from sklearn.decomposition import PCA
pca = PCA(whiten=True)
whitened = pca.fit_transform(X)
可以查看一下文档。
26
这里有一段用numpy实现的代码,目的是对矩阵进行“白化”,这个代码是我从这里找到的。
import numpy as np
def whiten(X,fudge=1E-18):
# the matrix X should be observations-by-components
# get the covariance matrix
Xcov = np.dot(X.T,X)
# eigenvalue decomposition of the covariance matrix
d, V = np.linalg.eigh(Xcov)
# a fudge factor can be used so that eigenvectors associated with
# small eigenvalues do not get overamplified.
D = np.diag(1. / np.sqrt(d+fudge))
# whitening matrix
W = np.dot(np.dot(V, D), V.T)
# multiply by the whitening matrix
X_white = np.dot(X, W)
return X_white, W
你也可以通过SVD的方法来对矩阵进行白化:
def svd_whiten(X):
U, s, Vt = np.linalg.svd(X, full_matrices=False)
# U and Vt are the singular matrices, and s contains the singular values.
# Since the rows of both U and Vt are orthonormal vectors, then U * Vt
# will be white
X_white = np.dot(U, Vt)
return X_white
第二种方法虽然稍微慢一点,但可能在计算上更稳定。