我正在尝试创建一个PCA脚本,它遍历一组图像,并将它们分解成按功率/重量排序的PC。据我所知,你想做M = U*S*V.T
,我已经在下面做了。我切断了N处的数组,这是一个过滤掉所有下层PC的过滤器。但它似乎根本不起作用,并返回了明显不是PC的。我是否误解了数学?在
def PCA(cube_array, PCA_N=False, cutoff=1, verbatim=0):
cube_shape_z, cube_shape_x, cube_shape_y = cube_array.shape
n = 1
M = cube_array.reshape(cube_array.shape[0],-1)
U, s, Vt = np.linalg.svd(M, full_matrices=False)
V = Vt.T
S = np.diag(s)
N = cutoff
Mhat = np.dot(U[:, :N], np.dot(S[:N, :N],
V[:,:N].T)).reshape(cube_array.shape)
return Mhat
主成分就是V的前N列
你计算的是矩阵M在2-范数和Frobenius范数下的最佳秩N逼近。在
相关问题 更多 >
编程相关推荐