實現pca降維-Python實現
阿新 • • 發佈:2020-07-10
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