#绘制不同学习率变化下iters-loss曲线for i inrange(5):
theta=np.matrix(np.zeros((3,1)))#初始化theta,避免前面的theta影响
a=[0.01,0.03,0.09,0.18,0.54]
theta,loss=GredientDescent(X,y,theta,iters=100,a=a[i])
plt.plot(range(101),loss,label='{}{}{}{}'.format('α',i,'=',a[i]))
plt.legend(loc='best')
<matplotlib.legend.Legend at 0x2afc8e334f0>
sklearn实现
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
df1=pd.read_csv('ex1data2.txt',header=None,
names=['siez of housing ','numbers of bedroom','house price'])
df1.head()