1. 程式人生 > >機器學習之線性回歸

機器學習之線性回歸

read 權重矩陣 add spl 比較 nic 影響 lib com

目錄

  • 預測數值型數據:回歸
    • 用線性回歸找到最佳擬合直線
      • 程序8-1 標準回歸函數和數據導入函數
      • 程序8-2 基於程序8-1繪圖
      • 圖片8-1 ex0的數據集和它的最佳擬合直線
    • 局部加權線性回歸
      • 圖片8-2 參數k與權重的關系
      • 程序8-3 局部加權線性回歸函數
      • 圖片8-3 局部加權線性回歸結果
    • 示例:預測鮑魚的年齡
    • 縮減系數來“理解”數據
      • 嶺回歸
      • 前向逐步回歸
    • 權衡偏差與方差
    • 示例:預測樂高玩具套裝的價格
      • 收集數據:使用 Google 購物的 API
      • 訓練算法:建立模型
    • 本章小結

預測數值型數據:回歸

代碼中涉及的數學公式可以自己下載 Typora 這款軟件後,把內容復制到.md文件內通過 Typora 打開


本章內容

  • 線性回歸
  • 局部加權線性回歸
  • 嶺回歸和逐步線性回歸
  • 預測鮑魚年齡和樂高玩具價格

前面的章節給大家介紹了監督學習的分類部分,接下來幾章將會帶領同學們翺翔浩瀚的回歸海洋,註意此回歸不是 Logistic 回歸(Logistic 回歸之所以取名為這是因為歷史遺留問題)。具體是什麽,那就開始讓我們來揭秘吧!
註意: 分類的目標變量是標稱型數據;回歸的目標變量是連續性數據。

用線性回歸找到最佳擬合直線

  • 線性回歸的優缺點:
    ? 優點:結果易於理解,計算上不復雜
    ? 缺點:對非線性的數據擬合不好
    ? 適用數據類型:數值型和標稱型數據
  • 回歸的目的:預測數值型的目標值
  • 預測汽車功率大小的計算公式:
    功率 = 0.0015 * 耗油量 + 0.99 * 百米加速時長 (純屬虛構,請勿模仿)
  • 回歸方程:上述計算公式即回歸方程
  • 回歸系數:上述計算公式中的0.0015和0.99
  • 預測值:給定所有待輸入的特征值乘以對應的回歸系數的總和
  • 非線性回歸:輸出為輸入的乘積,例:功率 = 0.0015 * 耗油量 * 百米加速時長
  • 回歸的一般方法:
    1. 收集數據:采用任意方法收集數據
    2. 準備數據:回歸需要數值型數據,標稱型數據將被轉成數值型數據
    3. 分析數據:可視化數據,采用縮減法求得新回歸系數後繪圖再與上一張圖比較
    4. 訓練算法:找到合適的回歸系數
    5. 測試算法:使用 R^2^或者預測值和數據的擬合度,來分析模型的效果
    6. 使用算法:使用回歸預測連續性數據的類別標簽
  • 矩陣x:輸入的所有數據
  • 向量 w:與數據對應的回歸系數
  • 預測結果 Y~1~:$Y_1={X^T}_1w?$
  • 平方誤差:$\sum_{i=1}^m(y_i-{x^T}_iw)^2$
  • 矩陣表示平方誤差:$(y-Xw)^T(y-Xw)$
  • 平方誤差對 w 求導:$X^T(Y-Xw)$
  • 平方誤差對 w 求導等於零得:$\hat{w}=(X^TX)-1X^Ty$

技術分享圖片

  • w 上方的標記含義:當前可以估計出 w 的最優解,即 w 的一個最佳估計
  • 上述公式包含$(X^TX)^{-1}$,即該方程中的 X 必須存在逆矩陣
    註意:不要糾結於公式,這不會影響你學習機器學習

程序8-1 標準回歸函數和數據導入函數

# coding: 'utf-8'
import os
import numpy as np
import matplotlib.pyplot as plt
from path_settings import machine_learning_PATH

data_set_path = os.path.join(machine_learning_PATH, '第八章/data-set')
ex0_path = os.path.join(data_set_path, 'ex0.txt')
ex1_path = os.path.join(data_set_path, 'ex1.txt')
abalone_path = os.path.join(data_set_path, 'abalone.txt')


def load_data_set(filename):
    # 文本第一行值全為0的解釋:簡單說是因為兩個矩陣相乘一個矩陣的行和另一個矩陣的列得相等,具體可查資料
    num_feat = len(open(filename).readline().split('\t')) - 1

    data_mat = []
    label_mat = []

    fr = open(filename)
    for line in fr.readlines():
        line_arr = []
        cur_line = line.strip().split('\t')
        for i in range(num_feat):
            line_arr.append(float(cur_line[i]))
        data_mat.append(line_arr)
        label_mat.append(float(cur_line[-1]))

    return data_mat, label_mat


def stand_regres(x_arr, y_arr):
    x_mat = np.mat(x_arr)
    y_mat = np.mat(y_arr)
    x_tx = x_mat.T * x_mat

    # 判斷矩陣是否為奇異矩陣,即矩陣是否有逆矩陣
    if np.linalg.det(x_tx) == 0:
        print("奇異矩陣沒有逆矩陣")
        return

    ws = x_tx.I * (x_mat.T * y_mat.T)

    # 求解未知矩陣
    # ws = np.linalg.solve(x_tx,x_mat.T*y_mat.T)

    return x_mat, y_mat, ws


def test_stand_regres():
    x_arr, y_arr = load_data_set(ex0_path)
    _, _, ws = stand_regres(x_arr, y_arr)
    print(ws)


if __name__ == '__main__':
    test_stand_regres()

程序8-2 基於程序8-1繪圖

def plot_stand_regres(x_mat, y_mat, ws):
    fig = plt.figure()
    ax = fig.add_subplot(111)
    ax.scatter(x_mat[:, 1].flatten().A[0], y_mat.T[:, 0].flatten().A[0])
    x_copy = x_mat.copy()
    x_copy.sort(0)
    y_hat = x_copy * ws
    ax.plot(x_copy[:, 1], y_hat)
    plt.show()


def test_plot_stand_regres():
    x_arr, y_arr = load_data_set(ex0_path)
    x_mat, y_mat, ws = stand_regres(x_arr, y_arr)
    plot_stand_regres(x_mat, y_mat, ws)

    # 判斷擬合效果
    print(np.corrcoef((x_mat * ws).T, y_mat))
    '''
        [[1.         0.98647356]
        [0.98647356 1.        ]]
    '''


if __name__ == '__main__':
    # test_stand_regres()
    test_plot_stand_regres()

圖片8-1 ex0的數據集和它的最佳擬合直線

技術分享圖片

局部加權線性回歸

  • 局部加權線性回歸:給待預測點附近的每個點賦予一定的權重
  • 局部加權線性回歸求回歸系數公式:$\hat{w}=(X^TWX)^{-1}X^TWy$ 技術分享圖片

  • W:給每個數據點賦予權重的矩陣
  • LWLR使用“核”(類似於支持向量機中的核)來對附近的點賦予更高的權重。
  • 最常用的核——高斯核:$w(i,i)=exp\left({\frac{|x^{(i)}-x|}{-2k^2}}\right)$ 技術分享圖片
  • 點 x 與 x(i)越近,w(i,i)將會越大,參數 k 決定了對附近的點賦予多大的權重。

圖片8-2 參數k與權重的關系

技術分享圖片

  • 假定我們正預測的點是 x=0.5,最上面的是原始數據集,第二個圖顯示了當 k=0.5 時,大部分數據都用於訓練回歸模型;最下面的圖顯示當 k=0.01 時,僅有很少的局部點被用於訓練回歸模型。

程序8-3 局部加權線性回歸函數

def lwlr(test_point, x_arr, y_arr, k=1):
    """給樣本點增加權重,參數 k 控制衰減的速度"""
    x_mat = np.mat(x_arr)
    y_mat = np.mat(y_arr)

    m = np.shape(x_mat)[0]

    # 創建對角權重矩陣。該矩陣對角線元素全為1,其余元素全為0
    weights = np.mat(np.eye(m))

    for j in range(m):
        diff_mat = test_point - x_mat[j, :]
        weights[j, j] = np.exp(diff_mat * diff_mat.T / (-2 * k ** 2))

    x_tx = x_mat.T * (weights * x_mat)

    if np.linalg.det(x_tx) == 0:
        print("奇異矩陣沒有逆矩陣")
        return

    ws = x_tx.I * (x_mat.T * (weights * y_mat.T))

    return test_point * ws


def lwlr_test(test_arr, x_arr, y_arr, k=1):
    """使數據集中每個點調用 lwlr 方法"""
    m = np.shape(test_arr)[0]

    y_hat = np.zeros(m)

    for i in range(m):
        y_hat[i] = lwlr(test_arr[i], x_arr, y_arr, k)

    return y_hat


def test_lwlr_test():
    x_arr, y_arr = load_data_set(ex0_path)
    y_hat = lwlr_test(x_arr, x_arr, y_arr, 0.003)
    print(y_hat)


def plot_lwlr(x_sort, y_hat, str_ind, x_mat, y_mat):
    fig = plt.figure()
    ax = fig.add_subplot(111)

    ax.plot(x_sort[:, 1], y_hat[str_ind])

    ax.scatter(x_mat[:, 1].flatten().A[0], y_mat.T[:, 0].flatten().A[0], s=2, c='red')

    plt.show()


def test_plot_lwlr():
    x_arr, y_arr = load_data_set(ex0_path)
    x_mat = np.mat(x_arr)
    y_mat = np.mat(y_arr)
    y_hat = lwlr_test(x_arr, x_arr, y_arr, 0.01)
    str_ind = x_mat[:, 1].argsort(0)
    x_sort = x_mat[str_ind][:, 0, :]

    plot_lwlr(x_sort, y_hat, str_ind, x_mat, y_mat)


if __name__ == '__main__':
    # test_stand_regres()
    # test_plot_stand_regres()
    # test_lwlr_test()
    test_plot_lwlr()

圖片8-3 局部加權線性回歸結果

技術分享圖片

示例:預測鮑魚的年齡

縮減系數來“理解”數據

嶺回歸

前向逐步回歸

權衡偏差與方差

示例:預測樂高玩具套裝的價格

收集數據:使用 Google 購物的 API

訓練算法:建立模型

本章小結

==尊重原創==
==可以伸出你的小手點個關註,謝謝!==

博客園地址:https://www.cnblogs.com/chenyoude/
github 地址:https://github.com/nickcyd/machine_learning
微信:a1171958281

機器學習之線性回歸