import numpy as npclassPCA:def__init__(self,n_components):"""初始化PCA"""assert n_components >=1,"n_components must be valid" self.n_components = n_components self.components_ =Nonedeffit(self,X,eta=0.01,n_iters=1e4):"""获取数据集的前n个主成分"""assert self.n_components <= X.shape[1],"n_components must not be greater than the feature number of X"defdemean(X):return X - np.mean(X, axis=0)deff(w,X):return np.sum((X.dot(w)**2))/len(X)defdf(w,X):return X.T.dot(X.dot(w))*2./len(X)# 把向量单位化defdirection(w):return w / np.linalg.norm(w)deffirst_component(X,initial_w,eta,n_iters=1e4,epsilon=1e-8): w =direction(initial_w) cur_iter =0while cur_iter < n_iters: gradient =df(w, X) last_w = w w = w + eta * gradient w =direction(w)if(abs(f(w, X))-abs(f(last_w, X))< epsilon):break cur_iter +=1return w X_pca =demean(X) self.components_ = np.empty(shape = (self.n_components, X.shape[1]))for i inrange(self.n_components): initial_w = np.random.random(X.shape[1]) eta =0.001 w =first_component(X_pca, initial_w, eta) self.components_[i,:]= w X_pca = X_pca - X_pca.dot(w).reshape(-1, 1)* wreturn selfdeftransform(self,X):"""将给定的X,映射到各个主成分分量中"""assert X.shape[1]== self.components_.shape[1]return X.dot(self.components_.T)definverse_transform(self,X):"""将给定的X反向映射回原来的特征空间"""assert X.shape[1]== self.components_.shape[0]return X.dot(self.components_)def__repr__(self):return"PCA(n_components=%d)"% self.n_components
使用PCA降维
准备数据
import numpy as npimport matplotlib.pyplot as pltX = np.empty((100,2))X[:,0]= np.random.uniform(0., 100., size=100)X[:,1]=0.75* X[:,0]+3.+ np.random.normal(0, 10., size=100)