# -*- 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()
批量梯度下降算法公式:
每一步更新都要对所有m个训练样本求和,得到新的θ后,用新的θ再一次对m个样本求和,得到新的θ。共有n个输入特征。
矩阵运算更快,利用矩阵的乘法直接进行了求和运算。x是m行n+1列的矩阵,第一列的值为1(x0为1)。
α的调整:计算每一次迭代的cost function的值,如果逐渐减小,可增大α;如果逐渐增大,则减小α。如果θ值震荡而不是收敛,则可能是α太大了。