import numpy as np
import matplotlib.pyplot as plt
def loadDataSet(fileName,delim = '\t'):
fr = open(fileName)
stringArr = [line.strip().split(delim) for line in fr.readlines()]
datArr = [list(map(float, line)) for line in stringArr]
datArr = np.mat(datArr)[:, 1:]
m, n = np.shape(datArr)
return datArr, m, n
datArr, m ,n = loadDataSet("ex0.txt",delim = "\t")
def regressionFunc(a, b, x): #求解預測值
y_ = a + b * x
return y_
def lossFunc(a, b, m): #代價函數
y = datArr[:, 1]
y_ = regressionFunc(a, b, datArr[:,0])
J = (1 / (2 * m)) * np.sum(np.square(y_ - y))
return J
def aGrad(a, b, datArr, m): #a的偏導數
y = datArr[:, 1]
a = (1 / m) * np.sum(regressionFunc(a, b, datArr[:, 0])-y)
return a
def bGrad(a, b, datArr, m): #b的偏導數
y = datArr[:, 1]
b = (1 / m) * np.sum(np.multiply((regressionFunc(a, b, datArr[:, 0])-y), datArr[:, 0]))
return b
def gradientFunc(a, b, datArr, alfa, iter): #梯度下降法
tempJ = np.zeros((iter, 1))
finishIter = iter
for i in range(iter):
tempJ[i] = lossFunc(a, b, m)
tempa = a - alfa * aGrad(a, b, datArr, m)
tempb = b - alfa * bGrad(a, b, datArr, m)
lasta = a
lastb = b
a = tempa
b = tempb
if lasta == a and lastb == b:
finishIter = i
break
return a, b, tempJ, finishIter
a, b, tempJ, finishIter = gradientFunc(2, 2, datArr, 0.01, 10000)
x = np.linspace(0, 1, 1000)
y = a + b * x
fig = plt.figure()
ax1 = fig.add_subplot(121)
ax1.scatter(datArr[:, 0].tolist(), datArr[:, 1].tolist(), edgecolors='k')
ax1.plot(x, y, color='r', linewidth=3)
plt.xlabel("x")
plt.ylabel("y")
plt.grid()
ax2 = fig.add_subplot(122)
ax2.plot(tempJ[:finishIter], linewidth=2.5)
plt.xlabel("Iteration Times")
plt.ylabel("Cost Function")
plt.grid()
plt.show()
誤差平方和的平均值爲: 0.0034
當學習速率取0.1時,代價函數相比學習速率爲0.01時收斂的更快一些
當學習速率取1時,代價函數相比學習速率爲0.1時收斂的更快了,並且當學習速率大於1時代價函數開始發散。所以要合理設置學習速率的值,過小收斂速度過慢迭代次數多,過大容易發散不能得到迴歸結果。