实现一元线性回归拟合-梯度下降实现python

对于给定训练集
x = [4,8,5,10,12]
y = [20,50,30,70,60]
参数的定义以及设置
#初始化参数
theta0 = theta1 = 0
#学习率
alpha = 0.00001
#迭代次数
cnt = 0
error0 = error1 = 0
#指定一个阈值,用于检查两次误差
thershold = 0.0000001
迭代进行

while True:
    #定义梯度 diff[0]-theta0 diff[1]-theta1
    diff = [0,0]
    m = len(x)
    for i in range(m):
        diff[0] = y[i] - (theta0+theta1*x[i])
        diff[1] =(y[i] - (theta0+theta1*x[i]))*x[i]
    theta0 = theta0 + alpha * diff[0]
    theta1 = theta1 + alpha * diff[1]
    #误差计算
    for i in range(m):
        error1 += (y[i] - (theta0+theta1*x[i]))**2
    error1 /= m
    if abs(error1 - error0) < thershold:
        break
    else:error0 = error1
    cnt += 1
    pass
print('theta0',theta0)
print('theta1',theta1)
print('cnt',cnt)
结果预测
print(predict(theta0,theta1,8))

你可能感兴趣的:(实现一元线性回归拟合-梯度下降实现python)