ML_梯度下降

具体的数学推导可以参照这一篇博客

http://www.cnblogs.com/pinard/p/5970503.html

#批量梯度下降  
#拟合函数:y = theta0 + theta1 * x  
#损失函数: j = 1 / 2 * sum (y(x) - y) ^ 2  
import matplotlib.pyplot as plt  
#造数据  
x_train = [150,200,250,300,350,400,600]
y_train = [6450,7450,8450,9450,11450,15450,18450]

  
#数据初始化  
pace = 0.000000001       #步长
theta0 = 0             #01
theta1 = 1             #02
epsilon = 0.00001    #收敛值
  
#样本的个数  
m = len(x_train)
#想要迭代次数
want = 5000
n = 1

  
def y(x):  
    return theta0 + theta1 * x  
  
#开始迭代  
while 1:  
    theta0_der = 0  
    theta1_der = 0  
    for i in range(m):  
         theta0_der += y(x_train[i]) - y_train[i]  
         theta1_der += (y(x_train[i]) - y_train[i]) * x_train[i]  
    theta0 -= pace * theta0_der
    theta1 -= pace * theta1_der

    print(theta0,theta1)         #打印每一次迭代的值
    n += 1;

      
    #判断是否收敛
    if (abs(pace * theta0_der) <= epsilon and abs(pace * theta1_der) <= epsilon) or want == n:
        break
      
#数据可视化  
plt.plot(x_train,[y(x) for x in x_train])  #训练出的一条直线是拟合的结果
plt.plot(x_train,y_train,'bo')
plt.show()

运行结果

# 梯度下降
# 拟合函数:y = theta0 + theta1 * x
# 损失函数: j = 1 / 2 * sum (y(x) - y) ^ 2
import matplotlib.pyplot as plt
import random

# 造数据
x_train = [150, 200, 250, 300, 350, 400, 600]
y_train = [6450, 7450, 8450, 9450, 11450, 15450, 18450]

# 数据初始化
pace = 0.000001  # 步长
theta0 = 1  # 01
theta1 = 1  # 02
epsilon = 0.0001  # 收敛值

# 样本的个数
m = len(x_train)
# 想要迭代次数
want = 5000
n = 1


def y(x):
    return theta0 + theta1 * x


# 开始迭代
while 1:
    theta0_der = 0
    theta1_der = 0
    i = random.randint(0,m-1)
    theta0_der += y(x_train[i]) - y_train[i]
    theta1_der += (y(x_train[i]) - y_train[i]) * x_train[i]

    theta0 -= pace * theta0_der
    theta1 -= pace * theta1_der

    print(theta0, theta1)  # 打印每一次迭代的值
    n += 1;

    # 判断是否收敛
    if (abs(pace * theta0_der) <= epsilon and abs(pace * theta1_der) <= epsilon) or want == n:
        break

# 数据可视化
plt.plot(x_train, [y(x) for x in x_train])  # 训练出的一条直线是拟合的结果
plt.plot(x_train, y_train, 'bo')
plt.show()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值