theta=np.zeros(2)
x
theta
iterations=1500
alpha=0.01
i=1
cost_com=np.empty(0)
while i<iterations:
for j in range(len(theta)):
theta[j]-=alpha*(hypothe(x,theta)-y).dot(x.iloc[:,j])/m
cost_com=np.hstack((cost_com,cost(hypothe(x,theta),y)))
i+=1
cost_com
theta
Ones
Population
0
1
6.1101
1
1
5.5277
2
1
8.5186
3
1
7.0032
4
1
5.8598
...
...
...
92
1
5.8707
93
1
5.3054
94
1
8.2934
95
1
13.3940
96
1
5.4369
97 rows × 2 columns
3.Visualizing J
fig=plt.figure()
axe=fig.add_axes([0.1,0.1,1.,1.])
axe.set_title('Cost function')
axe.set_xlabel('the cost')
axe.set_ylabel('the time of iterations')
axe.plot(cost_com)
cost(hypothe(x,theta),y)
4.483134519095647
tx=np.vstack((np.ones(1000),np.linspace(5,30,1000))).T
fig=plt.figure()
axe=fig.add_axes([0.1,0.1,1.,1.])
axe.set_title('the Result of Linear Regression')
axe.set_xlabel('the population')
axe.set_ylabel('the porfit')
axe.plot(np.linspace(5,30,1000),tx.dot(theta),'r')
axe.scatter(x1,y1)
Exercise 2: Linear Regression with multiple variables
1.Feature normalization
data=pd.read_csv('ex1data2.txt',names=['sizes','bedrooms','price'],header=None)
data.head()
data.describe()
mean_std=pd.DataFrame([],columns=['mean','std'],index=['sizes','bedrooms'])
for i in range(2):
mean_std.iloc[i,:]=[data.iloc[:,i].mean(),data.iloc[:,i].std()]
data.iloc[:,i]=(data.iloc[:,i]-data.iloc[:,i].mean())/data.iloc[:,i].std()
mean_std
m=len(X)
iterations=1500
alpha=0.01
i=1
cost_com_multi=np.empty(0)
while i<iterations:
for j in range(n):
theta[j]-=alpha*(hypothe_multi(theta,X)-y).dot(X[:,j])/m
cost_com_multi=np.hstack((cost_com_multi,cost_multi(theta,X,y)))
i=i+1
fig=plt.figure()
axe=fig.add_axes([0.1,0.1,0.8,0.8])
axe.set_xlabel('the cost')
axe.set_ylabel('the time of iterations')
axe.set_title('the multi-variable linear regression')
axe.plot(cost_com_multi)