我正在使用 Python 并使用以下方法实现了 PCA本教程.
一切都很好,我得到了协方差,我做了一个成功的变换,把它带到原来的尺寸不是问题。
但如何进行美白呢?我尝试将特征向量除以特征值:
S, V = numpy.linalg.eig(cov)
V = V / S[:, numpy.newaxis]
并使用 V 来转换数据,但这导致了奇怪的数据值。
有人可以解释一下吗?
这是我从以下网站获得的一些用于矩阵白化的 Matlab 代码的 numpy 实现here.
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
第二种方法有点慢,但可能在数值上更稳定。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)