手写二元线性回归梯度下降—Python代码

梯度下降法的python实现

  • 代码思路
  • 代码实现
  • 数据资料

代码思路

  1. 导入所需库
  2. 导入所需数据
  3. 数据转换—提取X,Y
  4. 设置初始值
    事先指定系数值、学习率、迭代次数
  5. 定义损失函数(二元线性回归)
    J ( θ i ) = 1 2 m ∑ i = 1 m [ h θ ( x ( i ) ) − y ( i ) ] 2 J(\theta_i)=\frac{1}{2m}\sum_{i=1}^{m}[h_\theta(x^{(i)})-y^{(i)}]^2 J(θi)=2m1i=1m[hθ(x(i))y(i)]2其中 h θ ( x ( i ) ) = θ 0 + θ 1 x ( 1 ) + θ 2 x ( 2 ) h_\theta(x^{(i)})=\theta_0+\theta_1x^{(1)}+\theta_2x^{(2)} hθ(x(i))=θ0+θ1x(1)+θ2x(2)
  6. 求解梯度下降
    ∂ J ( θ i ) ∂ θ i = 1 m ∑ i = 1 m ( h θ ( x ( i ) ) − y ( i ) ) ∗ x ( i ) = 1 m ∑ i = 1 m ( θ 0 + θ 1 x ( 1 ) + θ 2 x ( 2 ) − y ( i ) ) ∗ x ( i ) \frac{\partial J(\theta_i)}{\partial\theta_i}=\frac{1}{m}\sum_{i=1}^{m}(h_\theta(x^{(i)})-y^{(i)})*x^{(i)}=\frac{1}{m}\sum_{i=1}^{m}(\theta_0+\theta_1x^{(1)}+\theta_2x^{(2)}-y^{(i)})*x^{(i)} θiJ(θi)=m1i=1m(hθ(x(i))y(i))x(i)=m1i=1m(θ0+θ1x(1)+θ2x(2)y(i))x(i)

代码实现

  1. 导入所需库
import numpy as np 
  1. 导入所需数据
data = np.genfromtxt("线性回归/data/Delivery.csv",delimiter=',')
  1. 数据转换——提取X,Y
X = data[:,:-1]
Y = data[:,-1]
  1. 设置初始值
theta0 = 0
theta1 = 0
theta2 = 0
epochs = 100
alpha = 0.001
  1. 定义损失函数(根据上述公式编写)
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
  1. 定义梯度下降(根据上述公式编写)
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
  1. 输出结果(系数值和损失值)
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

你可能感兴趣的:(python,机器学习)