实现一元线性回归拟合-梯度下降实现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))
  • 6
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值