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)plt.scatter(X[:,0], X[:,1])plt.show()
第一步:demean
defdemean(X):return X - np.mean(X, axis=0)X_demean =demean(X)plt.scatter(X_demean[:,0], X_demean[:,1])plt.show()
第二步:梯度上升法
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