Andrew Ng機器學習——線性迴歸(Linear Regression)和批處理梯度下降(BGD)

線性迴歸和梯度下降算法

關於線性迴歸和梯度下降算法,簡單的說就是指這一類模型:輸出是連續值,並且其假設函數是線性函數,所以其cost function很容易求偏導數。利用梯度下降的方法來求參數(局部最優解)。關於這一類算法的介紹推薦以下幾個博客:
線性迴歸及梯度下降
BGD和SGD
本篇博客主要是分享一些可視化的python代碼

python實現

首先是一個多特徵的線性迴歸代碼

# encoding:utf-8
# 作者:FC
# 日期:2017/12/25

# 數據集(Train DataSet)
x = [(1, 0, 3), (1, 1, 3), (1, 2, 3), (1, 3, 2), (1, 4, 4)]
y = [95.364, 97.217205, 75.195834, 60.105519, 49.342380]

# 設置迭代停止的條件
epsilon = 0.0001
max_itor = 10000

# 設置學習速率,迭代初始誤差,迭代次數初始theta參數
alpha = 0.01
error_pre = 0
error = 0
cnt = 0
theta0 = 0; theta1 = 0;  theta2 = 0
m = len(x) # 數據集的長度
while True:
    cnt += 1
    error = 0
    # 計算代價函數
    for i in range(m):
        error += (theta0*x[i][0] + theta1*x[i][1] + theta2*x[i][2] - y[i])**2/(2*m)
    if abs(error - error_pre)<epsilon:
        break
    else:
        error_pre = error
    # 更新參數
    temp0=0;temp1=0;temp2=0
    for i in range(m):
        temp0 += (theta0*x[i][0]+theta1*x[i][1]+theta2*x[i][2]-y[i])*x[i][0]/m
        temp1 += (theta0 * x[i][0] + theta1 * x[i][1] + theta2 * x[i][2] - y[i]) * x[i][1] / m
        temp2 += (theta0 * x[i][0] + theta1 * x[i][1] + theta2 * x[i][2] - y[i]) * x[i][2] / m
    theta0 = theta0 - alpha*temp0
    theta1 = theta1 - alpha*temp1
    theta2 = theta2 - alpha*temp2

    print('theta0: %f, theta1: %f, theta2: %f, error: %f ' % (theta0,theta1,theta2,error))

print('Done: theta0: %f,theta1: %f, theta2: %f, error: %f'% (theta0,theta1,theta2,error))
print('迭代次數: %d' % cnt)





運行結果如下:
這裏寫圖片描述

爲了讓整個過程更爲形象,利用matplotlib庫寫了一個動態的一元線性迴歸實例,代碼如下:

# -*-coding:utf-8
# Author:FC
# Date:2017/12/26
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation
x = [1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 6.0]
y = [64.5, 74.5, 84.5, 94.5, 114.5, 154.5, 184.5]
Parm = []


# 設置初始參數
eps = 0.0001  # 迭代閾值
alpha = 0.01  # 學習速率
theta0 = 0
theta1 = 0  # 初始theta
cnt = 0  # 迭代次數
error = 0
error_pre = 0
m = len(x)  # 訓練集數量

def cost_func(a, b, theta0_new, theta1_new):
    J = 0  # 損失函數初始歸零
    for i in range(len(a)):
        J += (theta0_new + theta1_new*a[i] - b[i])**2
    J = J/(2*len(a))
    return J

def Update_theta (a, b, theta0_new, theta1_new):
    k0 = 0
    k1 = 0
    theta0_update = 0
    theta1_update = 0
    for i in range(len(a)):
        k0 += (theta0_new + theta1_new*a[i] - b[i])
        k1 += (theta0_new + theta1_new*a[i] - b[i])*a[i]
    k0 = k0/len(a)
    k1 = k1//len(a)
    theta0_update = theta0_new - alpha*k0
    theta1_update = theta1_new - alpha*k1
    return theta0_update, theta1_update


while True:
    Parm.append([theta0,theta1])
    cnt += 1
    error = cost_func(x, y, theta0, theta1)
    if abs(error-error_pre) < eps:
        break
# 更新參數
    else:
        error_pre = error
        theta0, theta1 = Update_theta(x, y, theta0, theta1)


x_plot = np.arange(0.20, 0.1)
fig = plt.figure()
ax = plt.axes()
line, = ax.plot([], [], 'g', lw=2)
label = ax.text([], [], '')


def init():
    line.set_data([],[])
    plt.plot(x, y, 'ro')
    # plt.axis([-6, 6, -6, 6])
    plt.grid(True)
    plt.xlabel('x')
    plt.ylabel('y')
    plt.title('Linear Regression')
    return line, label

def animate(i):
    x1 = 0
    x2 = 18
    y1 = Parm[i][0]+x1*Parm[i][1]
    y2 = Parm[i][0] + x2 * Parm[i][1]
    line.set_data([x1, x2], [y1, y2])
    return line, label
anim = animation.FuncAnimation(fig, animate, init_func=init, frames= 100, interval=200, repeat=False, blit=True)

plt.show()
anim.save('Regression.gif', fps=4, writer='imagemagick')



結果如下:
這裏寫圖片描述

發佈了44 篇原創文章 · 獲贊 65 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章