线性回归梯度下降

这篇博客介绍了如何用Python实现线性回归的梯度下降法。通过加载'l3_data.csv'数据集,计算并可视化损失(loss)曲线。在每次迭代中更新权重w和截距b,最终展示经过梯度下降后的b和w值。
摘要由CSDN通过智能技术生成

import matplotlib.pyplot as plt
from matplotlib import pyplot as plt
import numpy as np
import torch
import pandas as pd

%matplotlib inline

%config InlineBackend.figure_format = ‘svg’

绘制 loss 曲线

train_loss_results = [] # 将每轮的loss记录在此列表中,为后续画loss曲线提供数据

points = np.genfromtxt(‘l3_data.csv’,delimiter = ‘,’)
plt.scatter(points[:,0],points[:,1],c = ‘’,edgecolors = ‘b’,s = 15)
plt.show()

y = wx + b

def compute_error_for_line_given_points(b, w, points):
“”"
计算给定超参数[w,b]的误差值
“”"
totalError = 0
for i in range(0, len(points)):
x = points[i, 0]
y = points[i, 1]
totalError += (y - (w * x + b)) ** 2
return totalError / float(len(points))

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值