Liner Regression 線性迴歸及Python代碼

來源:互聯網
上載者:User

標籤:process   資料   weight   需要   之間   AC   lib   div   最小值   

  線性迴歸是最典型的迴歸問題,其目標值與所有的特徵之間存線上性關係。線性迴歸於羅吉斯迴歸類似,不同的是,羅吉斯迴歸線上性迴歸的基礎上加了邏輯函數,從而將線性迴歸的值從實數域映射到了0-1,通過設定閥值,便實現了迴歸的0-1分類,即二分類。

  線性迴歸函數$Y=XW$,其中Y是1*n維向量,X是n*m矩陣,W是m*1的係數矩陣。線性迴歸採用平方損失函數,至於為什麼採用平方損失函數,是因為平方損失函數採用的是最小二乘法的思想,其殘差滿足常態分佈的最大似然估計,詳情可百度。

  線性迴歸損失函數:${{l}_{w}}=\sum\limits_{i=1}^{n}{{{\left( {{y}_{i}}-X_{i}W \right)}^{2}}}$,其中$X_i$是1*m維向量,W是m*1維向量。

  線性迴歸的解法有很多種,如直接最小二乘法,梯度下降法和牛頓法等。

1. 最小二乘法

  直接最小二乘法是利用矩陣變換,直接求得係數向量W的矩陣解,過程如下。

  預測函數為:$Y=XW$,其損失函數表示為:${{Y-XW}^{T}}(Y-XW)$  

  對W求導可得:$\frac{d}{dW}{{Y-XW}^{T}}(Y-XW)={{X}^{T}}(Y-XW)$,其求導過程需要矩陣求導的知識。

  另導數為0,得到:$W={{\left( {{X}^{T}}X \right)}^{-1}}{{X}^{T}}Y$

2. 梯度下降法

  線性迴歸損失函數:${{l}_{w}}=\sum\limits_{i=1}^{n}{{{\left( {{y}_{i}}-X_{i}W \right)}^{2}}}$,要求損失函數的最小值,對參數W求偏導,得:

\[\frac{\partial {{\text{l}}_{w}}}{\partial W}=\sum\limits_{i=1}^{n}{\left( {{y}_{i}}-{{X}_{i}}W \right)}\cdot {{X}_{i}}\]

  由上式可知,參數W的梯度和羅吉斯迴歸中類似,是每個樣本的殘差值乘以樣本對應的值,然後累加起來,第$j$個參數的梯度是對所有樣本第$j$特徵執行上述操作。

 

  線性迴歸最小二乘和梯度下降法Python代碼如下:

 

# -*- coding: utf-8 -*-"""Created on Fri Jan 19 13:29:14 2018@author: zhang"""import numpy as npfrom sklearn.datasets import load_bostonimport matplotlib.pyplot as pltfrom sklearn.cross_validation import train_test_splitfrom sklearn import preprocessing"""多元線性迴歸需要對各變數進行標準化,因為在求係數wj梯度時,每個樣本計算值與其標籤值的差要與每個樣本對應的第j個屬性值相乘,然後求和因此,如果屬性值之間的差異太大,會造成係數無法收斂"""#  最小二乘法直接求解權重係數def least_square(train_x, train_y):     """     input:訓練資料(樣本*屬性)和標籤     """       weights = (train_x.T * train_x).I * train_x.T * train_y     return weights# 梯度下降演算法def gradient_descent(train_x, train_y, maxCycle, alpha):          numSamples, numFeatures = np.shape(train_x)     weights = np.zeros((numFeatures,1))          for i in range(maxCycle):          h = train_x * weights          err = h - train_y                     weights = weights - (alpha * err.T * train_x).T       return weights def stochastic_gradient_descent(train_x, train_y, maxCycle, alpha):     numSamples, numFeatures = np.shape(train_x)     weights = np.zeros((numFeatures,1))          for i in range(maxCycle):          for j in range(numSamples):               h = train_x[j,:] * weights               err = h - train_y[j,0]                          weights = weights - (alpha * err.T * train_x[j,:]).T                      return weightsdef load_data():     boston = load_boston()     data = boston.data     label = boston.target     return data, labeldef show_results(predict_y, test_y):     plt.scatter(np.array(test_y), np.array(predict_y), marker=‘x‘, s= 30, c=‘red‘)   #  畫圖的資料需要是數組而不能是矩陣     plt.plot(np.arange(0,50),np.arange(0,50))     plt.xlabel("original_label")     plt.ylabel("predict_label")     plt.title("LinerRegression")     plt.show()     if __name__ == "__main__":     data, label = load_data()     data = preprocessing.normalize(data.T).T          train_x, test_x, train_y, test_y = train_test_split(data, label, train_size = 0.75, random_state = 33)     train_x = np.mat(train_x)     test_x = np.mat(test_x)     train_y = np.mat(train_y).T   #   (3,)轉為矩陣變為行向量了,需要轉置     test_y = np.mat(test_y).T #     weights = least_square(train_x, train_y)#     predict_y = test_x * weights#     show_results(predict_y, test_y)#        weights = gradient_descent(train_x, train_y, 1000, 0.01)     predict_y = test_x * weights     show_results(predict_y, test_y)     #     weights = stochastic_gradient_descent(train_x, train_y, 100, 0.01)#     predict_y = test_x * weights#     show_results(predict_y, test_y)

 

Liner Regression 線性迴歸及Python代碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.