PCA:主成分分析-Python实现,X:[2500,784],把X降到50维
1 def pca(X=np.array([]), no_dims=50): 2 """ 3 Runs PCA on the NxD array X in order to reduce its dimensionality to 4 no_dims dimensions. 5 """ 6 7 print("Preprocessing the data using PCA...") 8 (n, d) = X.shape 9 X = X - np.tile(np.mean(X, 0), (n, 1)) # np.mean(X,0)在列上求均值 10 (l, M) = np.linalg.eig(np.dot(X.T, X)) 11 Y = np.dot(X, M[:, 0:no_dims]) 12 return Y
np.linalg.eig(np.dot(X.T, X)):
eig方法:计算方阵的特征值和右特征向量。
l:(784,):特征值
M:(784,784):右特征向量
Y = X*M
补:
参考:
https://blog.csdn.net/baimafujinji/article/details/79407488