二元线性回归之梯度下降法

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

# 读入数据
Data = genfromtxt(r"data2.csv", delimiter=',')

# 切分数据
X_data = Data[:, :-1]  # 除最后一列所有列
Y_data = Data[:, -1]  # 最后一列

# 学习率
LR = 0.0001

# 参数
Theta0 = 0
Theta1 = 0
Theta2 = 0

#
Epochs = 1000000


# 最小二乘法
def compute_error(theta0, theta1, theta2, x_data, y_data):
    total_error = 0
    for i in range(0, len(x_data)):
        total_error += (y_data[i] - ((theta1 * x_data[i, 0] + theta2 * x_data[i, 1]) + theta0)) ** 2
    return total_error / float(len(x_data)) / 2


def gradient_descent_runner(x_data, y_data, theta0, theta1, theta2, lr, epochs):
    # 计算数据总量
    m = float(len(x_data))

    # 计算导数
    for i in range(epochs):
        theta0_grad = 0
        theta1_grad = 0
        theta2_grad = 0
        for j in range(0, len(x_data)):
            theta0_grad += (1 / m) * (theta1 * x_data[j, 0] + theta2 * x_data[j, 1] + theta0 - y_data[j])
            theta1_grad += (1 / m) * x_data[j, 0] * (theta1 * x_data[j, 0] + theta2 * x_data[j, 1] + theta0 - y_data[j])
            theta2_grad += (1 / m) * x_data[j, 1] * (theta1 * x_data[j, 0] + theta2 * x_data[j, 1] + theta0 - y_data[j])
            # 更新参数
        theta0 -= lr * theta0_grad
        theta1 -= lr * theta1_grad
        theta2 -= lr * theta2_grad
    return theta0, theta1, theta2


Theta0, Theta1, Theta2 = gradient_descent_runner(X_data, Y_data, Theta0, Theta1, Theta2, LR, Epochs)
lose = compute_error(Theta0, Theta1, Theta2, X_data, Y_data)
print(Theta0, Theta1, Theta2)
print(lose)

ax = plt.figure().add_subplot(111, projection='3d')
ax.scatter(X_data[:, 0], X_data[:, 1], Y_data, c='r', marker='o', s=100)
x0 = X_data[:, 0]
x1 = X_data[:, 1]
x0, x1 = np.meshgrid(x0, x1)        # 生成方阵
z = Theta0 + x0 * Theta1 + x1 * Theta2
print("z is ", z)
print("x0  is ", x0)
print("x1  is ", x1)
# 画3d图
ax.plot_surface(x0, x1, z)
# 设置坐标轴
ax.set_xlabel('Miles')
ax.set_ylabel('Num of Deliveries')
ax.set_zlabel('Time')
plt.show()

在这里插入图片描述
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值