机器学习与数据挖掘--梯度下降法训练回归模型

机器学习与数据挖掘实验一

(使用梯度下降法训练回归模型)

实验目的:

掌握线性回归的基本原理,以及梯度下降法和最小二乘法

实验环境:

Anaconda/Jupyter notebook/Pycharm

实验内容:
  1. 编码实现基于梯度下降的单变量线性回归算法,包括梯度的计算与验证;
  2. 画数据散点图,以及得到的直线;
  3. 画梯度下降过程中损失的变化图;
  4. 基于训练得到的参数,输入新的样本数据,输出预测值;
实验步骤:
import numpy as np
import matplotlib.pyplot as plt

train = np.loadtxt('./data1.txt',delimiter=',',skiprows=1) #读取txt文件
train_x = train[:,0]
train_y = train[:,1]
train_x
type(train_x)
train_x.shape #获取矩阵的形状(元组类型)
#数据散点图
plt.plot(train_x,train_y,'o')
plt.show()

实验一1

# 初始化参数
theta0 = np.random.rand()
theta1 = np.random.rand()
# 预测函数
def f(x):
    return theta0 + theta1 * x
# 目标函数
def D(x,y):
    return 0.5 * np.sum((y-f(x))**2)
# 标准化
mu = train_x.mean()
sigma = train_x.std()
def standardize(x):
    return (x - mu)/sigma
train_z = standardize(train_x)
# 绘图
plt.plot(train_z,train_y,'o')
plt.show()

实验一2

ETA = 1e-3 # 学习率
diff = 1 #误差的差值
count = 0 #更新的次数
error = D(train_z,train_y)
while diff > 1e-2:
    tmp_theta0 = theta0 - ETA * np.sum((f(train_z)-train_y))
    tmp_theta1 = theta1 - ETA * np.sum((f(train_z)-train_y)*train_z)
    theta0 = tmp_theta0
    theta1 = tmp_theta1
    current_error = D(train_z,train_y)
    diff = error - current_error
    error = current_error #计算与上一次误差的差值
    # 输出日志
    count += 1
    log = '第 {} 次 : theta0 = {:.3f}, theta1 = {:.3f}, 差值 = {:.4f}'
    print(log.format(count, theta0, theta1, diff))

仅供参考数据

# 绘图
x = np.linspace(-1,3,100)
plt.plot(train_z,train_y,'o')
plt.plot(x,f(x))
plt.show()

实验一4

# 新的数据样本预测
f(standardize(100))
f(standardize(150))
f(standardize(200))
f(standardize(500))
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值