我正在尝试计算矩阵的 PCA。
有时,得到的特征值/向量是复值,因此当尝试通过将特征向量矩阵与点坐标相乘来将点投影到低维计划时,我得到以下警告
ComplexWarning: Casting complex values to real discards the imaginary part
在那行代码中np.dot(self.u[0:components,:],vector)
我用来计算 PCA 的整个代码
import numpy as np
import numpy.linalg as la
class PCA:
def __init__(self,inputData):
data = inputData.copy()
#m = no of points
#n = no of features per point
self.m = data.shape[0]
self.n = data.shape[1]
#mean center the data
data -= np.mean(data,axis=0)
# calculate the covariance matrix
c = np.cov(data, rowvar=0)
# get the eigenvalues/eigenvectors of c
eval, evec = la.eig(c)
# u = eigen vectors (transposed)
self.u = evec.transpose()
def getPCA(self,vector,components):
if components > self.n:
raise Exception("components must be > 0 and <= n")
return np.dot(self.u[0:components,:],vector)