梯度下降法实现线性回归

0.引入依赖

import numpy as np
import matplotlib.pyplot as plt

1.导入数据

points = np.genfromtxt('data.txt',delimiter = ',')
//提取points中的两列数据,分别作为x,y
x = points[:,0]
y = points[:,1]
//用plt画出散点图
plt.scatter(x,y)
plt.show()

运行结果

2.定义损失函数

//损失函数是系数的函数,另外还要传入数据的x,y
def compute_costs(w,b,points):
    total_cost = 0
    M = len(points)
    
    //逐点计算平方损失误差,然后求平均
    for i in range(M):
        x = points[i,0]
        y = points[i,1]
        total_cost += (y-w*x-b)**2
    return total_cost/M

3.定义模型超参数

alpha = 0.0001//下降的步长
init_w = 0
init_b = 0
num_iter = 10//相当于循环次数

4.定义核心梯度下降函数

def grad_desc(points, init_w, init_b, alpha, num_iter):
    w = init_w
    b = init_b
    //定义一个list保存所有损失函数的值,用来显示下降过程
    cost_list = []
    
    for i in range(num_iter):
        cost_list.append( compute_costs(w,b,points) )
        w, b = step_grad_desc( w, b, alpha, points )
        
    return [w, b, cost_list]

def step_grad_desc( current_w, current_b, alpha,points ):
    sum_grad_w = 0
    sum_grad_b = 0
    M = len(points)
    
    //对每个点代入公式求和
    for i in range(M):
        x = points[i,0]
        y = points[i,1]
        sum_grad_w += ( current_w * x + current_b - y ) * x
        sum_grad_b += current_w * x + current_b - y
        
    //用公式求当前梯度
    grad_w = 2/M * sum_grad_w
    grad_b = 2/M * sum_grad_b
    
    //梯度下降,更新当前的w,b
    updated_w = current_w - alpha * grad_w
    updated_b = current_b - alpha * grad_b
    
    return updated_w,updated_b

5.测试,运行梯度下降算法

w, b, cost_list = grad_desc(points, init_w, init_b, alpha, num_iter)

print("w is: ", w)
print("b is: ", b)

cost = compute_costs(w,b,points)
print("cost is: ", cost)

plt.plot(cost_list)
plt.show()

w is: 1.3889185526309822
b is: 0.02708960089671493
cost is: 98.70002273561147
运行结果

6.画出拟合曲线

plt.scatter(x, y)
//针对每一个x,计算出预测的y的值
pred_y = w * x + b

plt.plot(x, pred_y, c = 'r')
plt.show()

运行结果

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值