[机器学习]简单线性回归——梯度下降法

一.梯度下降法概念

2.代码实现

python 复制代码
# 0. 引入依赖
import numpy as np
import matplotlib.pyplot as plt

# 1. 导入数据(data.csv)
points = np.genfromtxt('data.csv', delimiter=',')
points[0,0]

# 提取points中的两列数据,分别作为x,y
x = points[:, 0]
y = points[:, 1]

# 用plt画出散点图
# plt.scatter(x, y)
# plt.show()

# 2. 定义损失函数:最小平方损失函数
# 损失函数是系数的函数,另外还要传入数据的x,y
def compute_cost(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 # 根据实际去调节
initial_w = 0 # 初始w,可以随机生成
initial_b = 0 # 初始b,可以随机生成
num_iter = 10 # 迭代次数

# 4. 定义核心梯度下降算法函数
def grad_desc(points, initial_w, initial_b, alpha, num_iter):
    w = initial_w
    b = initial_b
    # 定义一个list保存所有的损失函数值,用来显示下降的过程
    cost_list = []

    # 迭代,获取每次的损失函数值以及更行w、b
    for i in range(num_iter):
        cost_list.append(compute_cost(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
w, b, cost_list = grad_desc( points, initial_w, initial_b, alpha, num_iter )

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

cost = compute_cost(w, b, points)

print("cost is: ", cost)

# plt.plot(cost_list)
# plt.show() # 画图

# 6. 画出拟合曲线
plt.scatter(x, y)
# 针对每一个x,计算出预测的y值
pred_y = w * x + b

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

w is: 1.4774173755483797

b is: 0.02963934787473238

cost is: 112.65585181499748

3.代码及数据下载

简单线性回归-最小二乘法及梯度下降法资源-CSDN文库

相关推荐
九酒10 小时前
AI Agent 开发踩坑记:口播功能非得用 APP 原生实现吗?
前端·人工智能·agent
蝎子莱莱爱打怪10 小时前
DSpark 讲透:DeepSeek 不换模型,硬把 V4 提速 85%,是怎么做到的?
人工智能·面试·程序员
巫山老妖11 小时前
置身AI内
人工智能
IT_陈寒13 小时前
JavaScript项目实战经验分享
前端·人工智能·后端
vanuan14 小时前
两个AI智能体第一次对话-A2A双Agent协作实战
人工智能
kfaino16 小时前
码农的AI翻身(四)你好,我叫 Attention
人工智能·后端
雨落Re18 小时前
如何设计一个高质量Skill
人工智能
Token炼金师18 小时前
大模型权重文件全指南:从格式选择到优化实战
人工智能
阿牛哥_GX18 小时前
CDP 浏览器操控原理:让脚本接管你的浏览器
人工智能
ThreeS18 小时前
手搓MiniVLA全实战教程-一步一步用pytorch解释原理与思路
人工智能·python