代碼思路
- 導入所需庫
- 導入所需數據
- 數據轉換—提取X,Y
- 設置初始值
事先指定係數值、學習率、迭代次數
- 定義損失函數(二元線性迴歸)
J(θi)=2m1i=1∑m[hθ(x(i))−y(i)]2其中hθ(x(i))=θ0+θ1x(1)+θ2x(2)
- 求解梯度下降
∂θi∂J(θi)=m1i=1∑m(hθ(x(i))−y(i))∗x(i)=m1i=1∑m(θ0+θ1x(1)+θ2x(2)−y(i))∗x(i)
代碼實現
- 導入所需庫
import numpy as np
- 導入所需數據
data = np.genfromtxt("線性迴歸/data/Delivery.csv",delimiter=',')
- 數據轉換——提取X,Y
X = data[:,:-1]
Y = data[:,-1]
- 設置初始值
theta0 = 0
theta1 = 0
theta2 = 0
epochs = 100
alpha = 0.001
- 定義損失函數(根據上述公式編寫)
def cost(X,Y,theta0,theta1,theta2):
loss = 0
m = len(Y)
for i in range(m):
loss += (theta0+theta1*X[i,0]+theta2*X[i,1]-Y[i])**2
loss = loss/(2*m)
return loss
- 定義梯度下降(根據上述公式編寫)
def grad_des(X,Y,theta0,theta1,theta2,alpha,epochs):
m = len(Y)
for z in range(epochs):
theta0_grad = 0
theta1_grad = 0
theta2_grad = 0
for i in range(m):
theta0_grad = (theta0+theta1*X[i,0]+theta2*X[i,1]-Y[i])
theta1_grad = (theta0+theta1*X[i,0]+theta2*X[i,1]-Y[i])*X[i,0]
theta2_grad = (theta0+theta1*X[i,0]+theta2*X[i,1]-Y[i])*X[i,1]
theta0_grad = theta0_grad/m
theta1_grad = theta1_grad/m
theta2_grad = theta2_grad/m
theta0 -=alpha*theta0_grad
theta1 -=alpha*theta1_grad
theta2 -=alpha*theta2_grad
return theta0,theta1,theta2
- 輸出結果(係數值和損失值)
print('begin...theta0={},theta1={},theta2={},loss={}'.format(theta0,theta1,theta2,cost(X,Y,theta0,theta1,theta2)))
print('running...')
theta0,theta1,theta2 = grad_des(X,Y,theta0,theta1,theta2,alpha,epochs)
print('end...theta0={},theta1={},theta2={},loss={}'.format(theta0,theta1,theta2,cost(X,Y,theta0,theta1,theta2)))
數據資料
X1 |
X2 |
Y |
100 |
4 |
9.3 |
50 |
3 |
4.8 |
100 |
4 |
8.9 |
100 |
2 |
6.5 |
50 |
2 |
4.2 |
80 |
2 |
6.2 |
75 |
3 |
7.4 |
65 |
4 |
6 |
90 |
3 |
7.6 |
90 |
2 |
6.1 |