Ahm*_*otb 6 python numpy eigenvalue eigenvector pca
我试图计算矩阵的PCA.
有时,得到的特征值/向量是复数值,因此当试图通过将特征向量矩阵与点坐标相乘来将点投影到较低维度平面时,得到以下警告
ComplexWarning: Casting complex values to real discards the imaginary part
Run Code Online (Sandbox Code Playgroud)
在那行代码中 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)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3971 次 |
| 最近记录: |