ML-线性回归-批梯度下降

# -*- coding:utf-8 -*-
import numpy as np
from numpy import genfromtxt
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D


def get_data(data_set):
    m, n = np.shape(data_set)
    train_data = np.ones((m, n))
    train_data[:, 1:] = data_set[:, 1:]  # data_set[:, m:n]取所有行的第m到n-1列,含左不含右
    train_label = data_set[:, 0]  # 取data_set所有行第0列
    return train_data, train_label


def batchGradientDescent(x, y, theta, alpha, m, max_iterations):
    x_trans = x.transpose()
    for i in range(0, max_iterations):
        hypothesis = np.dot(x, theta)  # x--m行 2列(x0和x1);theta--2行(theta0和theta1)1列
        loss = hypothesis - y  # hypothesis,y--mx1
        gradient = np.dot(x_trans, loss)/m  # x_trans--2xm; loss--mx1; gradient--2x1
        theta = theta - alpha * gradient

        loss_trans = loss.transpose()  # loss_trans--1xm 行向量
        cost_f = np.dot(loss_trans, loss)  # 行x列=一个数
        cost_f /= 2
        print('iteration=', i, 'theta=', theta, 'cost=', cost_f)
    return theta


def cost_f(x, y, theta):  # 对每组特定的theta值,有唯一的cost_f值与之对应
    h = np.dot(x, theta)  # x 是theta的系数,y是实测值。(x,y)是m个训练样本
    loss = h - y
    loss_trans = loss.transpose()
    cost_f = np.dot(loss_trans, loss)
    cost_f /= 2
    return cost_f


# import graph from the files
data_path = r"C:\Users\TIANLI\Desktop\Dataset-HW01-Part-01.csv"
data_set = genfromtxt(data_path, delimiter=',')   # 原表格需去掉headings,去掉时间,去掉有空的数据,单元格式为数值
# train_data are x-values(temperatures)
# train_label are y- values (the power generated)
train_data, train_label = get_data(data_set)

m, n = np.shape(train_data)
alpha = 0.002
theta = np.zeros(n)
max_iterations = 50000
theta = batchGradientDescent(train_data, train_label,theta, alpha, m, max_iterations)
print('final values of theta is ', theta)  # the 1st is theta0 and 2nd is theta1


# plot the training data and the straight line
label = data_set[:, 0]
data = data_set[:, 1]
x = np.arange(10, 50, 0.1)
y = x*theta[1] + theta[0]
plt.scatter(data, label, marker='.')
plt.plot(x, y, color='black')
plt.xlabel('temperatures')
plt.ylabel('power')
plt.show()


# plot the cost function
theta0 = np.arange(0, 2000)
theta1 = np.arange(0, 2000)
X, Y = np.meshgrid(theta0, theta1)

cost = np.zeros((2000, 2000))
for i in range(0, 2000):
    for j in range(0, 2000):
         cost[i, j] = cost_f(train_data, train_label, [i, j])  # cost必须是二维的
         print(cost[i, j], i)

fig = plt.figure()
ax = Axes3D(fig)
ax.plot_surface(X, Y, cost, rstride=1, cstride=1, cmap='rainbow')
ax.set_zlabel('cost')
ax.set_ylabel('theta1')
ax.set_xlabel('theta0')
plt.show()

批量梯度下降算法公式:

 

  1.  每一步更新都要对所有m个训练样本求和,得到新的θ后,用新的θ再一次对m个样本求和,得到新的θ。共有n个输入特征。

  2. 矩阵运算更快,利用矩阵的乘法直接进行了求和运算。xmn+1列的矩阵,第一列的值为1x01)。

  3. α的调整:计算每一次迭代的cost function的值,如果逐渐减小,可增大α;如果逐渐增大,则减小α。如果θ值震荡而不是收敛,则可能是α太大了。



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值