classPCA: deffit(self, X): ''' fit PCA to X ''' n_sample, n_feature = X.shape self.mean_ = np.array([np.mean(X[:, i]) for i inrange(n_feature)]) X = X - self.mean_
# eigenvalue and eigenvector D, Q = np.linalg.eig(cov) # cov = QDQ^T eig_pairs = [(np.abs(D[i]), Q[:,i]) for i inrange(len(D))] eig_pairs.sort(key=lambda x: x[0], reverse=True)
self.feature_ = np.array([eig_pairs[i][1] for i inrange(n_feature)]).T
deftransform(self, X): X = X - self.mean_ return np.dot(X, self.feature_)
deftransform_k(self, X, k): ''' transform X to k-dimensional space ''' X = X - self.mean_ return np.dot(X, self.feature_[:,:k])
classPCA_SVD(PCA): deffit(self, X): ''' fit PCA to X ''' n_sample, n_feature = X.shape self.mean_ = np.array([np.mean(X[:, i]) for i inrange(n_feature)]) X = X - self.mean_
U,S,V_T = np.linalg.svd(X) D = (S**2)/(n_sample)
eig_pairs = [(np.abs(D[i]), V_T[:,i]) for i inrange(len(D))] eig_pairs.sort(key=lambda x: x[0], reverse=True)
self.feature_ = np.array([eig_pairs[i][1] for i inrange(n_feature)])
if __name__ == '__main__': # X = np.array([[-1,1],[-2,-1],[-3,-2],[1,1],[2,1],[3,2]]) # X = np.array([[-1,-2],[-1,0],[0,0],[2,1],[0,1]]) X = np.array([[-1,1,0],[-4,3,0],[1,0,2]])