机器学习与数据挖掘实验1-使用梯度下降法训练线性回归模型

 

记得自行导入数据文件data1.txt

import numpy as np
from matplotlib import pyplot as plt

train_data = np.genfromtxt('E:\\Downloads\\PyCharm 2022.3.2\\pythonProject1\\data1.txt', delimiter=',')
x_data = train_data[:, 0]
y_data = train_data[:, 1]

lr = 0.0001
theat0 = 0
theat1 = 0
iteators = 200


def compute_loss(x_data, y_data, theat0, theat1):
    sum = 0

    for i in range(len(x_data)):
        sum += pow((theat0 + theat1 * x_data[i]) - y_data[i], 2)

    return sum / (2 * len(x_data))


def gradient_fun(x_data, y_data, theat0, theat1, iteators, lr):
    m = float(len(x_data))

    cost = np.zeros(iteators)

    for j in range(iteators):
        sum1 = 0
        sum2 = 0
        for i in range(len(x_data)):
            sum1 += (theat0 + theat1 * x_data[i]) - y_data[i]
            sum2 += ((theat0 + theat1 * x_data[i]) - y_data[i]) * x_data[i]

        theat0 = theat0 - lr * sum1 / m
        theat1 = theat1 - lr * sum2 / m
        cost[j] = compute_loss(x_data, y_data, theat0, theat1)

        print('iteators = ', j, 'loss =', compute_loss(x_data, y_data, theat0, theat1), 'theat0 = ', theat0,
              'theat1 = ', theat1)

    return theat0, theat1, cost


theat0, theat1, cost = gradient_fun(x_data, y_data, theat0, theat1, iteators, lr)

plt.plot(x_data, y_data, 'b.')
plt.plot(x_data, theat0 + theat1 * x_data, 'r')
plt.show()

fig, ax = plt.subplots(figsize=(12, 8))
ax.plot(np.arange(iteators), cost, 'b')
ax.set_xlabel('Iterations')
ax.set_ylabel('Cost')
ax.set_title('cost__iteators')
plt.show()

pre_data = np.genfromtxt('E:\\Downloads\\PyCharm 2022.3.2\\pythonProject1\\data1.txt', delimiter=',')
x_data1 = pre_data[:, 0]


def predict_func(x_data1, theat0, theat1):

    pre_y = np.zeros(len(x_data1))
    for i in range(len(x_data1)):
        pre_y[i] = theat0 + theat1 * x_data[i]
        print("x  ", x_data[i], "预测值", pre_y[i])



predict_func(x_data1, theat0, theat1)

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值