机器学习算法基础——梯度下降法之二元线性回归

import numpy as np
import matplotlib.pyplot as plt
from numpy import genfromtxt
from mpl_toolkits.mplot3d import Axes3D # 可以用来画3D图

# 导入数据
data = genfromtxt(r"C:\\ML\\chapter-1\\Delivery.csv",delimiter=",")
print(data)

# 切分数据
x_data = data[:,:-1] # 行,列
y_data = data[:,-1] # 行,列
print(x_data)
print(y_data)

# 学习率 learning rate
lr = 0.0001

# 参数
theta0 = 0
theta1 = 0
theta2 = 0

# 最大迭代次数
epochs = 1000

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

def gradient_descent_runner(x_data,y_data,theta0,theta1,theta2,lr,epochs):
    # 计算总数据量
    m = float(len(x_data))
    # 循环epochs次
    for i in range(epochs):
        theta0_grad = 0 # 梯度0
        theta1_grad = 0 # 梯度1
        theta2_grad = 0 # 梯度2
        # 计算梯度总和再求平均
        for j in range(0,len(x_data)):
            theta0_grad += -(1/m) * (y_data[j] - (theta1*x_data[j,0] + theta2*x_data[j,1] + theta0))
            theta1_grad += -(1/m) * x_data[j,0] * (y_data[j] - (theta1*x_data[j,0] + theta2*x_data[j,1] + theta0))
            theta2_grad += -(1/m) * x_data[j,0] * (y_data[j] - (theta1*x_data[j,0] + theta2*x_data[j,1] + theta0))
        # 更新参数
        theta0 = theta0 - (lr*theta0_grad)
        theta1 = theta1 - (lr*theta1_grad)
        theta2 = theta2 - (lr*theta2_grad)
    return theta0,theta1,theta2

print('Staring theta0 = {0},theta1 = {1},theta2 = {2},error = {3}'.format(theta0,theta1,theta2,compute_error(theta0,theta1,theta2,x_data,y_data)))
print('Running...')
theta0,theta1,theta2 = gradient_descent_runner(x_data,y_data,theta0,theta1,theta2,lr,epochs) # 开始建模
print('After {0} iterations theta0 = {1},theta1 = {2},theta2 = {3}, error = {4}'.format(epochs,theta0,theta1,theta2,compute_error(theta0,theta1,theta2,x_data,y_data)))

ax = plt.figure().add_subplot(111,projection = "3d")
ax.scatter(x_data[:,0],x_data[:,1],y_data,c = 'r',marker = 'o',s = 100) # 点为红色三角形 s代表点的大小
x0 = x_data[:,0]
x1 = x_data[:,1]
# 生成网络矩阵
x0,x1 = np.meshgrid(x0,x1)
z = theta0 + x0*theta1 + x1*theta2
# 画3D图
ax.plot_surface(x0,x1,z)
# 设置坐标轴
ax.set_xlabel('Miles')
ax.set_ylabel("Num of Deliveries")
ax.set_zlabel('Time')

# 显示图像
plt.show()

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xiao黄

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值